A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/string-comparison-in-python/ below:

String Comparison in Python - GeeksforGeeks

String Comparison in Python

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:

Let's explore different methods to compare strings

== 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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4