How to Compare Strings in Python?

1 week ago 8
Club.noww.in

Recently, I chose the topic of “Python string comparison ” for a webinar. In this tutorial, I will explain how to compare strings in Python using different methods with examples.

To compare strings in Python, you can use basic comparison operators like ==, !=, <, >, <=, and >=. These operators compare strings lexicographically, based on the Unicode values of the characters. For instance, city1 == city2 checks if the strings city1 and city2 are equal, while city1 < city2 checks if city1 is lexicographically smaller than city2.

Basic Comparison Operators in Python

Python provides several operators for string comparison. The most used ones are ==, !=, <, >, <=, and >=. These operators compare strings lexicographically, meaning they compare based on the Unicode values of the characters.

Syntax and Description

  • ==: Checks if two strings are equal.
  • !=: Checks if two strings are not equal.
  • <: Checks if the string on the left is lexicographically smaller than the string on the right.
  • >: Checks if the string on the left is lexicographically greater than the string on the right.
  • <=: Checks if the string on the left is lexicographically smaller than or equal to the string on the right.
  • >=: Checks if the string on the left is lexicographically greater than or equal to the string on the right.

Examples

Now, let me show you some examples of how to compare strings in Python using the above operators.

# Example strings city1 = "New York" city2 = "Los Angeles" city3 = "Chicago" # Equality print(city1 == city2) # Output: False # Inequality print(city1 != city3) # Output: True # Lexicographical comparison print(city1 < city2) # Output: True print(city2 > city3) # Output: True print(city3 <= city1) # Output: True print(city3 >= city2) # Output: False

I executed the above Python code using VS code, and you can see the exact output in the screenshot below:

python compare strings

Check out Convert String to List in Python Without Using Split

Using the is and is not Operators

The is and is not operators compare the identities of two strings, not their values. This means they check if both strings are stored at the same memory location.

Syntax

  • is: Checks if two strings are the same object.
  • is not: Checks if two strings are not the same object.

Examples

Let me show you some examples of string comparison in Python.

# Example strings state1 = "California" state2 = "Texas" state3 = state1 # Identity comparison print(state1 is state2) # Output: False print(state1 is state3) # Output: True print(state1 is not state2) # Output: True

Here is the exact output in the screenshot below:

python string comparison

Using the str.lower() and str.upper() Methods

Sometimes, you need to compare strings in Python in a case-insensitive manner. You can achieve this by converting both strings to lowercase or uppercase before comparison.

Syntax

  • str.lower(): Converts all characters in the string to lowercase.
  • str.upper(): Converts all characters in the string to uppercase.

Examples

Now, let me show you a few examples of string comparisons using str.lower() and str.upper() methods.

# Example strings company1 = "Apple" company2 = "apple" company3 = "Microsoft" # Case-insensitive comparison print(company1.lower() == company2.lower()) # Output: True print(company1.upper() == company2.upper()) # Output: True print(company1.lower() == company3.lower()) # Output: False

Here is the exact output in the screenshot below:

python string compare

Read Find the First Number in a String in Python

Using the in Operator

The in operator checks if a substring exists within another string in Python. This is useful for partial matches.

Syntax

  • in: Checks if a substring is present in the string.
  • not in: Checks if a substring is not present in the string.

Examples

Let me show you some examples.

# Example strings sentence = "The quick brown fox jumps over the lazy dog" word1 = "quick" word2 = "slow" # Substring check print(word1 in sentence) # Output: True print(word2 not in sentence) # Output: True

I executed the above Python code, and you can see the output in the screenshot below:

string comparison in python

Read Reverse a String in Python

Using the str.startswith() and str.endswith() Methods

The str.startswith() and str.endswith() methods are used to check if a string starts or ends with a specific substring.

Syntax

  • str.startswith(substring): Checks if the string starts with the specified substring.
  • str.endswith(substring): Checks if the string ends with the specified substring.

Examples

Let me show you some examples now.

# Example strings filename = "report2024.pdf" prefix = "report" suffix = ".pdf" # Prefix and suffix check print(filename.startswith(prefix)) # Output: True print(filename.endswith(suffix)) # Output: True

Here is the exact output in the screenshot below:

python compare string

Conclusion

In this tutorial, I explained how to compare strings in Python using various methods and operators. Whether you need to check for equality, identity, or partial matches, Python provides different methods to handle these tasks efficiently.

Feel free to share your thoughts or questions in the comments below!

You may also like the following tutorials:

Read Entire Article