Comparing Strings

The equality and inequality operators can be used on strings as well. For example, the greater than and less than operators compare alphabetical order.

 

Assume you have str1  as "Rahul"  and str2  as "Ramesh" . The first two characters of both strings are R and R  are compared. They are equal, now the second two characters are compared i.e a and a. Since they are also equal, the third two characters i.e h  and m  are compared. As  'm'  has higher ASCII value than 'h' , str2  is greater than str1

            print("Is Apple ==  Apple ? ", "Apple" == "Apple")

print("Is Apple !=  apple ? ", "Apple" != "apple")

print("Is Apple ==  APple ? ", "Apple" == "APple")

print("Is Apple < APple ? " , "Apple" < "APple")

print("Is ApplE > Apple ? ", "Apple" > "ApplE" )

print("Is habit <= Habit ? ", "habit" <= "Habbit")