7.8. String comparison

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

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

Activity: CodeLens 7.8.2 (strComparisonCodelens)

Python does not handle uppercase and lowercase letters the same way that people do. All uppercase letters come before all lowercase letters, so “Pineapple” is less than “banana”, even though “pineapple” is greater than “banana”.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

You have attempted 1 of 6 activities on this page