Python String Comparison

Python | String comparison: In this tutorial, we are going to learn how strings are compared in Python programming languages? Here, we are explaining string comparison with some of the examples. By IncludeHelp Last updated : January 03, 2024

Introduction to Python String Comparison

Python string comparison compares two strings and determines whether they are equal or not or how these two strings are related to each other, and returns the Boolean result (True/False). The string comparison is done with the help of the Python equality operators (== and !=) and the comparison operators (<, >, <=, >=).

In this tutorial, we will learn how string comparison works with the help of examples.

String Comparison using Equality Operators

You can compare the string using Python's equality operators which are Equal To (==) and Not Equal To (!=).

  • The Equal To (==) operators returns True if both strings are equal; False, otherwise.
  • The Not Equal To (!=) operators return True if both strings are not equal; False, otherwise.

Example

Comparing two strings using equality operators.

str1 = "IncludeHelp"
str2 = "includehelp"
str3 = "IncludeHelp"

# Using of == Operator
if str1 == str2:
    print(str1, "is equal to", str2)
else:
    print(str1, "is not equal to", str2)

if str1 == str3:
    print(str1, "is equal to", str3)
else:
    print(str1, "is not equal to", str3)

# Using of != Operator
if str1 != str2:
    print(str1, "is not equal to", str2)
else:
    print(str1, "is equal to", str2)

if str1 != str3:
    print(str1, "is not equal to", str3)
else:
    print(str1, "is equal to", str3)

Output

The output of the above example is:

IncludeHelp is not equal to includehelp
IncludeHelp is equal to IncludeHelp
IncludeHelp is not equal to includehelp
IncludeHelp is equal to IncludeHelp

String Comparison using Comparison/Relational Operators

You can perform the lexicographical comparison on Python strings by using the Greater Than (>), Less Than (<, Greater Than or Equal To (>=), and Less Than or Equal To (<=) operators.

Example 1

Comparing a string variable with string value.

str1 = "IncludeHelp"

# comparison
print("str1 <  \"IncludeHelp\"): ", str1 < "IncludeHelp")
print("str1 <= \"IncludeHelp\"): ", str1 <= "IncludeHelp")
print("str1 >  \"IncludeHelp\"): ", str1 > "IncludeHelp")
print("str1 >= \"IncludeHelp\"): ", str1 >= "IncludeHelp")

Output

The output of the above example is:

str1 <  "IncludeHelp"):  False
str1 <= "IncludeHelp"):  True
str1 >  "IncludeHelp"):  False
str1 <= "IncludeHelp"):  True

Example 2

Comparing two string variables.

str1 = "IncludeHelp"
str2 = "includehelp"

# comparison
print("str1 <  str2: ", str1 < str2)
print("str1 <= str2: ", str1 <= str2)
print("str1 >  str2: ", str1 > str2)
print("str1 >= str2: ", str1 >= str2)

Output

The output of the above example is:

str1 <  str2:  True
str1 <= str2:  True
str1 >  str2:  False
str1 >= str2:  False

Comparing User Input with Equality and Comparison Operators

You can also perform the comparison operation on the user input. To take string input from the user, use the input() method.

Example

In this example, we are taking input two strings, and comparing them.

# input two strings
str1 = input("Enter string 1: ")
str2 = input("Enter string 2: ")

# comparing by equality operators
if str1 == str2:
    print("Both strings are equal.")

if str1 != str2:
    print("Both strings are not equal.")

# comparing by comparison operators
if str1 < str2:
    print(str1, "comes before", str2)
elif str1 > str2:
    print(str1, "comes after", str2)
else:
    print(str1, "and", str2, "are same")

Output

The output of the above example is:

Enter string 1: Hello World
Enter string 2: Hello World
Both strings are equal.
Hello World and Hello World are same

RUN 2:
Enter string 1: Honda Amaze
Enter string 2: Honda City
Both strings are not equal.
Honda Amaze comes before Honda City

RUN 3:
Enter string 1: BMW
Enter string 2: AUDI
Both strings are not equal.
BMW comes after AUDI

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.