String Comparison in Python (original) (raw)
Last Updated : 29 Oct, 2024
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.
Python `
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)
`
**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
- != Operator for Inequality Check
- Lexicographical Comparison
- Case-Insensitive Comparison
- Using startswith() and endswith() Methods
== 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.
Python `
s1 = "Python" s2 = "Python"
Since both strings are identical, therefore it is True
print(s1 == s2)
`
**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.
Python `
s1 = "Python" s2 = "Java"
"Python" is different from "Java", therefore it is True
print(s1 != s2)
`
**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.
Python `
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)
`
**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.
Python `
s1 = "Apple" s2 = "apple"
Both strings are same ignoring case, therefore it is True
print(s1.lower() == s2.lower())
`
**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.
Python `
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"))
`
**Explanation: These methods are helpful for conditional checks based on prefixes or suffixes in a string.
**Related Articles: