This is a detailed tutorial on comparing string in python. Learn to compare different strings on the basis of their characters, length, case, etc.
Table of Contents
Python String Comparison
Python allows you to make use of the different operators to compare strings. There are several numbers of factors on the basis of which you can compare two or more strings with each other. The most popular comparisons are Dicitonary listing and length comparison.
Dictionary comparison means to check which string will appear before the other in dictionary order. String length is quite a common comparison. It checks which string is larger than the other as per the number of characters included in both strings formation.
The following python comparison operators can be used to compare strings in python as well, apart from just comparing the numerical values.
Operator | Description (As per String Comparision) | ||
|
To compare if strings are equal. The strings need to have the same characters in the same case, in the same order to be equal and obviously the length should be the same. | ||
|
To compare if the strings are not equal. If even any of the parameters like the characters, their case or order, or length is different, the strings are not considered as equal. | ||
|
To check if the string1 appears before the string2 in the dictionary order. | ||
|
To check if the string1 appears after the string2 in the dictionary order. | ||
|
To check either the strings are equal or the string1 appears before the string2 in the dictionary order. | ||
|
To check either the strings are equal or the string1 appears after the string2 in the dictionary order. |
Note. All of the above-described operators will return a boolean value when applied to two string data types so as to form a boolean expression.
Examples
So, now you might have a basic understanding of how the different operators work for different types of strings comparison in python. Let’s have a look at some of the examples to understand the concept even better. For any of the string comparison, you’ve to use these operators inside If-Else Statements.
Equality Comparision
We can use the operators ==
and !=
to check if two strings are equal or not. As I mentioned, the strings need to be exactly the copy of another to get matched and to be considered as equal. The following example illustrates the use of these equality operators to compare different strings.
1 2 3 4 5 6 7 8 9 |
str1 = "WTMatter.com" str2 = "WtMatter.com" str4 = "WTMatter.com" if str1 == str4: print("str1 and str4 are equal.") if str1 != str2: print("str1 is not equal to str2.") |
All of the characters of the variable str1
and the variable str4
are exactly the same, in the same case and same order, therefore, they are equal. On the other hand, the second character of the variable str1
and str2
are different. There is a case difference, the str1
has a capital T while str2
has a small t.
Dictionary Order Comparison
Using the operators <
and >
, the comparisons of dictionary order can be done. In other words, using these operators we can check which string will come before and after when both of them will be arranged in dictionary order i.e. in alphabetical order. The following code snippets illustrate the use of less than and greater than operators for such a strings comparison.
1 2 3 4 5 6 7 |
str1 = "WTMatter.com" str3 = "WTMatter" if str3 < str1: print("str3 comes before str1.") if str1 > str3: print("str1 comes after str3.") |
Comparing Either Equality or Dictionary Order
The operators <=
and >=
can be used to compare either the two strings are equal or if they are occurring the desired dictionary order. The following example illustrates the same.
1 2 3 4 5 6 7 8 9 10 11 12 |
str1 = "WTMatter.com" str3 = "WTMatter" str4 = "WTMatter.com" if str3 <= str1: print("Either str3 comes before str1 in dictionary order or both of them are equal.") if str1 <= str4: print("Either str4 comes before str1 in dictionary order or both of them are equal.") if str1 >= str3: print("Either str1 comes before str3 in dictionary order or both of them are equal.") if str1 >= str3: print("Either str1 comes after str3 in dictionary order or both of them are equal.") |
String Length Comparision
You can also compare if the two strings have the same length or not regardless of which characters are included in the strings. For the length comparison, you can use the in-built len()
function. This function can be used to find out the length of any object like Python List rather than just the length of strings. As this function returns an integer value, so it can be used with any of the other comparison operators.
1 2 3 4 5 |
str1 = "WTMatter.com" str3 = "WTMatter" if(len(str1) > len(str3)): print("str1 is longer than str3.") |
Related Articles.
- Python String Replace – replace() Function
- Convert a List To String in Python
- How To Reverse A String In Python?
- How To Get Substring Of A String In Python?
- How to Split String in Python?
- Take Input In Python (Tutorial)
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!