Tutorials Tonight
Follow Us

String Comparison in Python


In this article, you will see how many ways you can compare strings in Python with some real-world examples.

Comparing strings can have many different aspects or purposes. For example:

You can use these comparisons to tell which one is lexicographically (alphabetically) larger than the other.

Let's look at all of these aspects in detail.


    Table Of Contents

  1. Compare String Using Comparison Operator
    1. Equal To ==
    2. Not Equal To !=
    3. Greater Than > & >=
    4. Less Than < & <=
  2. Compare String Using For Loop
  3. Part of another string (substring or not)
    1. Substring (in operator)
    2. Not Substring (not in operator)
  4. Prefix or Suffix of another string
    1. Prefix (startswith operator)
    2. Suffix (endswith operator)
  5. Conclusion

string comparison in python

Compare String Using Comparison Operator

Comparison operators in Python is used to compare two strings. It returns True or False based on the condition.

When we use it to compare two strings, internally it uses Unicode values of the strings to compare them. If Unicode values of all the characters in the strings are the same, then the string is considered equal.


1. Equal To ==

To compare if 2 strings are equal, we can use the == operator.

The == operator is case-sensitive.

For example:

# using == operator
print("Hello" == "Hello") # True

print("Hello" == "hello") # False

The above code will print True for the first comparison and False for the second comparison.

To compare without looking at the cases convert both strings to lower case using the lower() method.

# using == operator with lower() method
print("Hello".lower() == "hello".lower()) # True
print("HELLO".lower() == "HeLLo".lower()) # True

Note: You can also use __eq__() method to compare the equality of two strings. "A"__eq__("A") will return True.


2. Not Equal To !=

To compare if 2 strings are not equal, we can use != operator.

The != operator is case sensitive.

# using != operator
print("Hello" != "Hello") # False

print("Cat" != "dog") # True

3. Greater Than > & >=

Using the comparison operator we can also compare if a string is greater than another string or not.

For a string being greater than another string does not mean one has more characters than another string. It means that the dictionary position of one string is greater than the other string.

For example, B is after A in the dictionary so "B" > "A" will return True.

# using > operator
print("B" > "A") # True
print("B" >= "A") # True

print("A" > "B") # False
print("A" >= "B") # False

print("ABC" > "B") # False (length of string doesn't matter)

4. Less Than < & <=

A string less than another string means that the dictionary position of one string is less than the other string.

# using < operator
print("A" < "B") # True
print("A" <= "B") # True

print("B" < "A") # False
print("B" <= "A") # False

print("ABC" < "B") # True (length of string doesn't matter)

Note: To compare strings without looking at the cases convert both strings either to lower case (lower() method) or to upper case (upper() method).


Compare String Using For Loop

You can also use for loop to compare strings.

How to compare two strings in Python using for loop?🤔

The idea is to loop through the string and compare the corresponding characters of both strings. If all the characters are the same, then the string is considered equal.

# using for loop
str1 = "Hello"
str2 = "Hello"

for i in range(len(str1)):
    if str1[i] != str2[i]:
        print("False")
        break
else:
    print("True")

Stay Ahead, Learn More.


Part of another string (substring or not)

If a string is part of another string, means it is a substring of another string.

We can use in and not in operators to check if a string is part of another string.


1. in operator

in operator returns True if a string is part of another string.

The in operator is used between 2 strings and is case sensitive.

# using in operator
print("Hello" in "Hello World") # True

print("Cat" in "Hello World") # False

2. not in operator

not in operator returns True if a string is not part of another string.

The not in operator is used between 2 strings and is case sensitive.

# using not in operator
print("Hello" not in "Hello World") # False

print("Cat" in "Hello World") # True

Prefix or Suffix of another string

Prefix and suffix of a string can be checked using startswith() and endswith() methods respectively.

1. startswith() method

The startswith() method returns True if a string starts with a specified substring.

The startswith() method is case-sensitive.

# using startswith() method
print("Hello World".startswith("Hello")) # True

print("Hello World".startswith("hello")) # False

2. endswith() method

The endswith() method returns True if a string ends with a specified substring.

The endswith() method is case-sensitive.

# using endswith() method
print("Hello World".endswith("World")) # True

print("Hello World".endswith("world")) # False

Conclusion

String comparison in python is a very common task while working with strings. We have discussed many different types of comparisons between 2 strings in python.

We have also discussed how to compare strings using for loop.

Happy Coding 😇