As the name suggests, in Python string comparison we will identify whether the input strings are equal to each other or it can be also a find of greater and smaller among them.

The string comparison in Python can be done using multiple operators as shown below.

1Using ‘==’ (Equal to) Check whether the strings are
2Using ‘! =’ (Now Equal to) Check whether the strings are Not equal.
3Using ‘<‘ (Less than) Check whether the left string is smaller than right.
4Using ‘>’ (Greater than) Check whether the left string is greater than right.
5Using ‘<=’ (Less than or equal to) Check whether the left string is smaller than or equal to right.
6Using ‘>=’ (Greater than or equal to)Check whether the left string is greater than or equal to right.

Important:

  • The string comparison in Python is Case-Sensitive, which means it will treat the Upper and Lower characters as different ones.
  • And if the encounters the two same strings, then the Unicode value of the string is compared and lesser Unicode values are considered as a smaller value.
  • Unicode: Each character has a number ranging between 0 and 65,535. Suppose the value of character ‘A’ is 65 and ‘B’ is 66, which is greater than ‘A’.

String comparison using the ‘==’ (equal to) operator.

If the input two strings match each other or they are exactly same, then it should print the mentioned statement accordingly.

string_1 = input('Enter the first string: ')
string_2 = input('Enter the second string: ')

if string_1 == string_2:
    print('The first and second strings are equal')
else:
    print('The first and second strings are not equal')

Output =

Enter the first string: Praj
Enter the second string: Praj

The first and second strings are equal

As you can observe in the first output, because of the presence of same strings, they are equal.

String comparison using the ‘!=’ (Not equal to) operator.

If both the input strings are not matching each other, then the output will be ‘Not equal’.

string_1 = input('Enter the first string: ')
string_2 = input('Enter the second string: ')

if string_1 != string_2:
    print('The first and second strings are Not equal')
else:
    print('The first and second strings are equal')

The Output =

Enter the first string: Praj
Enter the second string: praj

The first and second strings are Not equal

As you already came across the fact that string comparison is case-sensitive, here in the above code, the input strings are case-different. Hence the output will be ‘Not equal’.

String comparison using ‘<‘ (less than) and ‘>’ (greater than) operators.

Let’s take on other examples to illustrate the comparison using above mentioned operators.

name_1 = input('Enter the name: ')
name_2 = input('Enter the name: ')

if name_1 < name_2:
    print(name_1 + ' ' + 'Comes earlier than' + ' ' + name_2)
elif name_1 > name_2:
    print(name_2 + ' ' + 'Comes earlier than' + ' ' + name_1)
else:
    print(name_1 + ' ' + name_2 +' ' + 'are equal')

Output =

Enter the name: Praj
Enter the name: Ravi

Praj Comes earlier than Ravi

Enter the name: Jarvis
Enter the name: Ravi

Ravi Comes earlier than Jarvis

Enter the name: Ajay
Enter the name: Ajay

Ajay Ajay are equal

You can observe the above code outputs, where we are comparing the strings using the operators. The code looks for the characters and case sensitivity and returns the output accordingly.

String compare using the sort() function.

Do you know that, you can even compare the unordered or random strings? well, you can do that easily with the help of sort() function in Python. Let’s see how it works.

name_1 = input('Enter the name: ')
name_2 = input('Enter the name: ')

if sorted(name_1) == sorted(name_2):
    print('The first and second strings are equal')
else:
    print('The first and second strings are not equal')

Enter the name: Machine learning
Enter the name: learning Machine

The first and second strings are equal

Did you wonder, how it returned the output as equal?

Well, there comes the function sort(). It sorts the strings or values for you, so that the mixed words will fall in a correct lane. In this case, you can see that Both the strings were same but they are mixed up. Hence you have to sort the values first and then pass it to the statements.

Wrapping Up

Python string comparison can be done through the basic python operators along with functions like sort(). Always keep in mind that the comparison is case-sensitive and if it encounters the same strings, it will go with the Unicode value, which is unique for each character.

That’s all for now! Happy comparing!!!

More read: Python documentation

Categorized in: