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

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 6 activities on this page