Lesson 27
Comparing text
After this lesson: You'll check a password and a command, and put words in alphabetical order.
Video
The video for this lesson is coming soon.
Explanation
So far we compared numbers. But == works on text too! That way a program can check a password, the answer to a riddle or a command for a robot. Careful: Python compares text letter by letter - and it's very precise! ๐
The club's secret password ๐ฌ
Click Next - you'll see that every letter counts for Python.
The dragon club
Bajtek starts a club ๐. The password is smok (Polish for dragon). A program stands at the door: if password == "smok": - let them in.
Letter by letter
Watch where the first difference is - that's what decides.
Click the arrow - we'll compare the texts letter by letter.
1password = "Smok"2print(password == "smok")3print(password == "Smok")4print(password != "smok")
โThe door to the dragon club ๐
Type the password ๐ and press Enter โ. Try Smok with a capital letter too!
Click the arrow - we'll compare the texts letter by letter.
1password = input()2if password == "smok":3print("Welcome to the club! ๐")4else:5print("Wrong password ๐")
โA trap: a number in quotes ๐ฌ
Click Next - why doesn't Python agree that 5 is 5?
Code
๐ฅ๏ธ Screen
Bajtek's riddle
Bajtek asks: "How many legs does a dog have... counting the tail? ๐". The program reads the answer: ans = input(). Kuba types 5. The program checks ans == 5... and says it's wrong!
Text or number?
Three comparisons of the same answer - only the first one gives False.
1ans = "5"2print(ans == 5)3print(ans == "5")4print(int(ans) == 5)
โA smaller text? The order of a dictionary
Text can also be compared with < and >. Python orders it like a dictionary: it compares the first letters, and when they're the same - the next ones. The first different letter decides: earlier in the alphabet = the "smaller" text.
| Comparison | Result | Why |
|---|---|---|
"kot" < "pies" | True | k comes before p |
"kot" < "koza" | True | ko = ko, then t before z |
"Ala" < "Ola" | True | A comes before O |
Fun fact: for Python all capital letters come before small ones, so "Zebra" < "ala" is True! In tasks all the words are usually written the same way (e.g. in small letters).
Bajtek's dictionary
Each comparison: the letters go in turn until the first different one turns up.
Click the arrow - we'll compare the texts letter by letter.
1print("kot" < "pies")2print("kot" < "koza")3print("Ala" < "Ola")4print("Zebra" < "ala")
โWho comes first in the register? ๐
Type two names ๐ง with a capital letter, each on its own line (e.g. Ola and Kuba). Enter โ after each.
Click the arrow - we'll compare the texts letter by letter.
1first = input()2second = input()3if first < second:4print(first)5else:6print(second)
โA remote for the robot ๐ฎ
Type a command ๐ฎ: lewo (left), prawo (right) or stop - and press Enter โ. And what happens with another word?
command = input()print("โฌ
๏ธ Turning left")print("โก๏ธ Turning right")print("๐ Standing still")print("โ I don't know that command")Click the arrow - I'll walk the program's path, line by line.
1command = input()2if command == "lewo":3print("โฌ ๏ธ Turning left")4elif command == "prawo":5print("โก๏ธ Turning right")6elif command == "stop":7print("๐ Standing still")8else:9print("โ I don't know that command")
โOnly == and != for answers
When you check a password, a command or an answer, use == and !=. The signs < and > are useful for putting words in alphabetical order.
What will this program print?
print("Kot" == "kot")
What will this program print?
print("abc" < "abd")
The input is 7. What does the condition ans == 7 give when ans = input()?
Remember
==and!=work on text - they compare character by character.- A capital and a small letter are different characters, and a space is a character too.
input()gives text: compare with"5"or useint(ans) == 5.<and>order text like a dictionary - the first different letter decides.
On to the tasks! This time you read words - no int().
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.