Home » Python

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.
Submitted by IncludeHelp, on April 13, 2020

Python | String comparison

In python programming language, strings can be compared using the relationship/ comparisons operators like ==, !=, <, >, <=, >=.

These operators need two operands (strings), checks the characters (based on its UNICODE values) of the string and return True or False.

Example 1:

str1 = 'IncludeHelp'

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

Output

str1 == 'IncludeHelp'):  True
str1 != 'IncludeHelp'):  False
str1 <  'IncludeHelp'):  False
str1 <= 'IncludeHelp'):  True
str1 >  'IncludeHelp'):  False
str1 >= 'IncludeHelp'):  True

Example 2:

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)
print("str1 >  str2: ", str1 >  str2)
print("str1 >= str2: ", str1 >= str2)

Output

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

Example 3:

str1 = 'IncludeHelp'
str2 = 'includehelp'
str3 = 'IncludeHelp'

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

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

Output

IncludeHelp is not equal to includehelp
IncludeHelp is equal to IncludeHelp

Example 4:

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

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

Output

RUN 1:
Enter string 1: Honda Amaze
Enter string 2: Honda City
Honda Amaze comes before Honda City

RUN 2:
Enter string 1: BMW
Enter string 2: Audi
BMW comes after Audi

RUN 3:
Enter string 1: BMW z4
Enter string 2: BMW z4
BMW z4 and BMW z4 are same





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.