Comparing Strings in Python

The easiest way to compare Python strings is to use the '==' and '!=' boolean operators. You can also compare strings using the 'is' and 'is not' operators, which are primarily used to compare objects in Python. Thus, you can also use the '<' and '>' operators to compare strings. In this Python Compare Strings example, we are comparing strings using the '==' operator. Other string comparison methods are provided below, with detailed examples and explanations. Click Execute to run the Python Compare Strings Example online and see the result.
Comparing Strings in Python Execute
first_string = 'first string'
second_string = 'second string'
print(first_string == second_string)
Updated: Viewed: 6055 times

What are strings in Python?

Internally, Python stores strings as an array of 16-bit Unicode bytes (or 8-bit ASCII bytes for Python 2), where each character in the string is represented by one byte. Python strings are immutable, which means they cannot be changed once created. All string processing methods return a copy of the string and do not modify the original. Python doesn't have a separate data type for a single character - it's just a string of length 1.

The built-in "str" library provides an extensive list of methods for searching, concatenating, reversing, splitting, comparing strings, and more.

String literals can be enclosed in single, double, or triple quotes. Single quotes are most commonly used, and you can use single quotes in double-quoted strings and vice versa. Triple quotes are frequently used to represent multiline strings and docstrings.

Comparing Python strings using the == and != operators

You can use the boolean operators "==" and "! =" to compare two strings. You can use the "==" operator to test strings for similarity and the "!=" operator to check strings for inconsistencies. Each of the operators will return a boolean value True or False, depending on the result.

Comparing two strings using "==" operator
user_input = 'apple'
password = 'apple'
print(password == user_input)

# True

Comparing two strings using "!=" operator
user_input = 'apple'
password = 'mango'
print(password != user_input)

# True

Comparing Python strings using the 'is' and 'is not' operators

You can also use the 'is' and 'is not' operators to compare strings. Unlike '==' and '!=', the comparison 'is' performed on the string id. To find out the string id, use the id() method. Usually, the is operator 'is' used to compare objects in Python, but you can compare strings in the same way.

Сompare Python strings using the 'is' operator
str_1 = 'banana'
str_2 = 'BANANA'
print(str_1 is str_2)

# False

Сompare Python strings using the 'is not' operator
str_1 = 'banana'
str_2 = 'BANANA'
print(str_1 is not str_2)

# True

Сompare Python strings conditions <, >, <=, >=

Between strings in Python, you can perform <, or > comparisons as well as between numbers. If the first line is a prefix of the second, then it is less than the second. If two lines start the same, but then differ, then less is the line with the first differing character less. This sorting order is called lexicographic (the same is used in dictionaries).

Python string comparison with 'less than' or 'greater than' operators
print('aaa' < 'aab')

# True

In Python, you can make multiple string comparisons at the same time.

Python multiple string comparison
print('123' < '2123' < '3123' < '4123')

# True

How to compare strings in a case-insensitive manner?

To perform a case-insensitive string comparison, you must first convert both strings to lower or upper case using the lower() or upper() string methods and then perform the string comparison. This is because lowercase and uppercase characters have different ASCII codes.

Python Case-insensitive String Comparison using the lower()
str_1 = 'apple'
str_2 = 'APPLE'

print(str_1 == str_2)
# False

print(str_1.lower() == str_2.lower())
# True

Python Case-insensitive String Comparison using the upper()
str_1 = 'cherry'
str_2 = 'CHERRY'

print(str_1 == str_2)
# False

print(str_1.upper() == str_2.upper())
# True

Conclusion

Python provides a rich set of methods for comparing strings. If you want to check for string equality, use the '==' or '!=' operators. If you want to compare strings as objects, use the 'is' and 'is not' operators. If you want to know which string is lexicographically smallest, use the '<', '>', '<=', '>=' operators. To perform a case-insensitive string comparison, convert both strings to lowercase before comparing.

See Also