String Comparison in Python
Python supports several operators for string comparison, including ==, !=, <, <=, >, and >=. These operators allow for both equality and lexicographical (alphabetical order) comparisons, which is useful when sorting or arranging strings.
Let’s start with a simple example to illustrate these operators.
s1 = "apple"
s2 = "banana"
# "apple" is not equal to "banana", therefore it is False
print(s1 == s2)
# "apple" is different from "banana", therefore it is True
print(s1 != s2)
# "apple" comes before "banana" lexicographically, therefore it is True
print(s1 < s2)
s1 = "apple"
s2 = "banana"
# "apple" is not equal to "banana", therefore it is False
print(s1 == s2)
# "apple" is different from "banana", therefore it is True
print(s1 != s2)
# "apple" comes before "banana" lexicographically, therefore it is True
print(s1 < s2)
Output
False True True
Explanation:
- == checks if two strings are identical.
- != checks if two strings differ.
- <, <=, >, >= perform lexicographical comparisons based on alphabetical order.
Let's explore different methods to compare strings
Table of Content
== Operator for Equality Check
The == operator is a simple way to check if two strings are identical. If both strings are equal, it returns True; otherwise, it returns False.
s1 = "Python"
s2 = "Python"
# Since both strings are identical, therefore it is True
print(s1 == s2)
s1 = "Python"
s2 = "Python"
# Since both strings are identical, therefore it is True
print(s1 == s2)
Output
True
Explanation: In this example, since s1 and s2 have the same characters in the same order, so == returns True.
!= Operator for Inequality Check
The != operator helps to verify if two strings are different. If the strings are different then it will return True, otherwise returns False.
s1 = "Python"
s2 = "Java"
# "Python" is different from "Java", therefore it is True
print(s1 != s2)
s1 = "Python"
s2 = "Java"
# "Python" is different from "Java", therefore it is True
print(s1 != s2)
Output
True
Explanation: Here, != checks that s1 and s2 are not the same, so it returns True.
Lexicographical Comparison
Lexicographical comparison checks if one string appears before or after another in alphabetical order. This is especially useful for sorting.
s1 = "apple"
s2 = "banana"
# "apple" appears before "banana" alphabetically, therefore it is True
print(s1 < s2)
# "banana" comes after "apple", therefore it is True
print(s2 > s1)
s1 = "apple"
s2 = "banana"
# "apple" appears before "banana" alphabetically, therefore it is True
print(s1 < s2)
# "banana" comes after "apple", therefore it is True
print(s2 > s1)
Output
True True
Explanation: The < and > operators are used to find the order of s1 and s2 lexicographically. This method ideal for sorting and alphabetical comparisons.
Case-Insensitive Comparison
Strings in Python can be compared case-insensitively by converting both strings to either lowercase or uppercase.
s1 = "Apple"
s2 = "apple"
# Both strings are same ignoring case, therefore it is True
print(s1.lower() == s2.lower())
s1 = "Apple"
s2 = "apple"
# Both strings are same ignoring case, therefore it is True
print(s1.lower() == s2.lower())
Output
True
Explanation: Converting both strings to lowercase (or uppercase) before comparison
Using startswith() and endswith() Methods
The startswith() and endswith() methods in Python are used to check if a string begins or ends with a specific substring.
s = "hello world"
# 's' starts with "hello", therefore it is True
print(s.startswith("hello"))
# 's' ends with "world", therefore it is True
print(s.endswith("world"))
s = "hello world"
# 's' starts with "hello", therefore it is True
print(s.startswith("hello"))
# 's' ends with "world", therefore it is True
print(s.endswith("world"))
Output
True True
Explanation: These methods are helpful for conditional checks based on prefixes or suffixes in a string.
Related Articles: