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:
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.
- True
- Correct! 'a' comes before 's'
- False
- Incorrect! Letters earlier in the alphabet are treated as less than letters later in the alphabet. Try again.
11-9-3: Evaluate the following comparison:
"dog" < "shark"
- True
- Incorrect! Strings are compared character by character. Try again.
- False
- Correct! Both strings match until the 'g', but Dog is shorter than Doghouse so it comes first in the dictionary.
11-9-4: Evaluate the following comparison:
"Dog" > "Doghouse"
- True
- Incorrect! Remember, uppercase letters come before lowercase letters. Try again.
- False
- Correct! In Python, uppercase letters are less than lowercase letters.
- They are the same word
- Incorrect! Python is case sensitive, meaning that uppercase characters and lowercase characters are treated as different. Try again.
11-9-5: Evaluate the following comparison:
"dog" < "Dog"
You have attempted of activities on this page