You are here

String comparison

2 September, 2015 - 17:48

The relational operators work on strings. To see if two strings are equal:

if word == 'banana':
    print 'All right, bananas.'

Other relational operations are useful for putting words in alphabetical order:

if word < 'banana':
    print 'Your word,'  + word + ', comes before banana.'
elif word > 'banana':
    print 'Your word,'  + word + ', comes after banana.'
else:
    print 'All right, bananas.

Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so:

Your word, Pineapple, comes before banana.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison. Keep that in mind in case you have to defend yourself against a man armed with a Pineapple.