Comparing strings in python

Comparing strings is a common job in programming. We can compare two strings with a == operator. We can check the opposite with the non-equality != operator. The operators return a boolean True or False.

>>> if "abc" == "abc": # comparing with if
    print "abc equals abc"
else:
    print "abc not equals abc"
    
abc equals abc
>>> print "aa" == "aa" # actual result will be returned in True and False
True
>>> print "aa" == "aaa"
False
>>> print 1==1
True
>>> print 1==2
False
Share

You may also like...