Skip to main content
Engineering LibreTexts

8.10: String comparison

  • Page ID
  • 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.')
        print('All right, bananas.')

    Python does not handle uppercase and lowercase letters the same way 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.

    • Was this article helpful?