// Tutorial //

Python String Comparison

Published on August 3, 2022
Default avatar
By Pankaj
Developer and author at DigitalOcean.
Python String Comparison

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.

Python String Comparison

Python string comparison is performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller. Let’s look through some examples for string comparison.

fruit1 = 'Apple'

print(fruit1 == 'Apple')
print(fruit1 != 'Apple')
print(fruit1 < 'Apple')
print(fruit1 > 'Apple')
print(fruit1 <= 'Apple')
print(fruit1 >= 'Apple')

Output:

True
False
False
False
True
True

Both the strings are exactly the same, hence they are equal. So equality operator is returning True in this case. Let’s look at another example where we will get inputs from the user and then compare them.

fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')

if fruit1 < fruit2:
    print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are same.")

Output:

Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.

Let’s see if the comparison is case sensitive or not? Also if ‘a’ comes ‘A’?

print('apple' == 'Apple')
print('apple' > 'Apple')
print('A unicode is', ord('A'), ',a unicode is', ord('a'))

Output:

False
True
A unicode is 65 ,a unicode is 97

So “Apple” is smaller when compared to “apple” because of their Unicode values. We are using ord() function to print the Unicode code point value of the characters. What if one of the string is made of second string and some additional characters?

print('Apple' < 'ApplePie')

Output: True If the characters sequence are the same in both the strings but one of them have some additional characters, then the larger length string is considered greater than the other one. What if we use < and > operators to compare two equal strings?

print('apple' < 'apple')
print('apple' > 'apple')

Output:

False
False

Obviously, both the strings are neither smaller nor greater than the other one. Hence the output is false in both the cases.

You can checkout complete python script and more Python examples from our GitHub Repository.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Pankaj

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

what if I want to get the difference in term of percentage.For instance , Apple and apple instead of getting false can I get a percentage of similarity like 93%

- Ahmed

    You missed one thing, if it’s ‘applebanana’ and ‘appleorange’ then ‘appleorange’ is greater than ‘applebanana’. Hopefully, this helps.

    - Akash

      when comparing strings, is only unicode of first letter considered or addition of unicodes of all the letters is considered?

      - BS

        print(‘Apple’ < ‘ApplePie’) does not return True because of the length. print(‘2’ < ‘11’) will return False.

        - Ammar S Salman

          your day of love may bring the gratitude of others for life.

          - Hobbes.Christine