How to compare Strings in Python

In this post, we will see how to compare Strings in Python.
You can simply use normal comparison operators to compare String in Python.

Let’s see with the help of example.

s1="John" s2="John" s3="john" print("If John==John:",s1==s2) print("If John==john:",s2==s3) #Case insensitive String comparison print("case insensitive: If John==john: ",s2.lower()==s3.lower()) #String comparison using if else s1="Apple" s2="Banana" if(s1 > s2): print("Apple comes after Banana") elif(s1 < s2): print("Apple comes before Banana") else: print("Apple and Banana are equal")

Output:

If John==John: True
If John==john: False
case insensitive: If John==john: True
Apple comes before Banana

That’s all about String comparison in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *