Skip to main content
Engineering LibreTexts

6.8: String Comparison

  • Page ID
    3095
  • The comparison operators work on strings. To see if two strings are equal:

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

    Other comparison 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.