What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Compare String in Python? (String Comparison 101)

  • Mar 20, 2021
  • 4 Minute Read
How to Compare String in Python? (String Comparison 101)

In this article, we will learn what is strings in a programming language, how to create them, and their uses. Further, we will study various operators to compare strings in python. At last, we will study some Python strings comparison in brief along with its python code example and output. So, let’s get started!

What are Strings?

A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding.  Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters.

Python String Comparison operators

In python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operator used for this purpose below:

  • ==: This operator checks whether two strings are equal.
  • !=: This operator checks whether two strings are not equal.
  • <: This operator checks whether the string on the left side is smaller than the string on the right side.
  • <=: This operator checks whether the string on the left side is smaller or equal to the string on the right side.
  • >: This operator checks whether the string on the left side is greater than the string on the right side.
  • >=: This operator checks whether the string on the left side is greater than the string on the right side.

String Equals Check in Python

In python programming we can check whether strings are equal or not using the “==” or by using the “.__eq__” function.

Example:

s1 = 'String'
s2 = 'String'
s3 = 'string'

# case sensitive equals check
if s1 == s2:
    print('s1 and s2 are equal.')

if s1.__eq__(s2):
    print('s1 and s2 are equal.')

Here, we check string s1 and s2 whether they are equal or not, and then use the “if” conditional statement with a combination of the equal operator.

The output of the above code is as given below:

 s1 and s2 are equal.

 s1 and s2 are equal.

What about Case insensitive comparisons?

While checking the equality in strings sometimes we wish to ignore the case of the string while comparison. So, as a solution to this, we can use the case fold(), lower(), or upper() function for ignoring the case insensitive comparison of string equality.

s1 = 'String'
s2 = 'String'
s3 = 'string'

if s1.casefold() == s3.casefold():
    print(s1.casefold())
    print(s3.casefold())
    print('s1 and s3 are equal in case-insensitive comparison')

if s1.lower() == s3.lower():
    print(s1.lower())
    print(s3.lower())
    print('s1 and s3 are equal in case-insensitive comparison')

if s1.upper() == s3.upper():
    print(s1.upper())
    print(s3.upper())
    print('s1 and s3 are equal in case-insensitive comparison')

The output of the above code is as given below:

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 STRING

 STRING

 s1 and s3 are equal in case-insensitive comparison

 

Conclusion

So, in this article, we studied how to compare strings in a python programming language. Also, we studied some string comparison operators and check string equality. Even we checked the string case insensitive comparison.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.