AlgoBazaAlgoBaza
Python for kids ยท Module 2

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.

Bajtek
๐Ÿšช"smok"๐Ÿ‰
1

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.

Step 1 / 5

Letter by letter

Watch where the first difference is - that's what decides.

k o t โ‡„ k o t

Click the arrow - we'll compare the texts letter by letter.

Code
1password = "Smok"
2print(password == "smok")
3print(password == "Smok")
4print(password != "smok")
Screen
โ–
1/5 ยท line 1 runs next

The door to the dragon club ๐Ÿ‰

Type the password ๐Ÿ”‘ and press Enter โŽ. Try Smok with a capital letter too!

k o t โ‡„ k o t

Click the arrow - we'll compare the texts letter by letter.

Code
1password = input()
2if password == "smok":
3 print("Welcome to the club! ๐Ÿ‰")
4else:
5 print("Wrong password ๐Ÿ”’")
Screen
โ–
1/2 ยท line 1 runs next

A trap: a number in quotes ๐ŸŽฌ

Click Next - why doesn't Python agree that 5 is 5?

?Bajtek
But it says 5!๐Ÿคจ

Code

ans = input()
print(ans == 5)

๐Ÿ–ฅ๏ธ Screen

5
False
1

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!

Step 1 / 3

Text or number?

Three comparisons of the same answer - only the first one gives False.

Code
1ans = "5"
2print(ans == 5)
3print(ans == "5")
4print(int(ans) == 5)
Screen
โ–
1/5 ยท line 1 runs next

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.

ComparisonResultWhy
"kot" < "pies"Truek comes before p
"kot" < "koza"Trueko = ko, then t before z
"Ala" < "Ola"TrueA 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.

k o t โ‡„ k o t

Click the arrow - we'll compare the texts letter by letter.

Code
1print("kot" < "pies")
2print("kot" < "koza")
3print("Ala" < "Ola")
4print("Zebra" < "ala")
Screen
โ–
1/5 ยท line 1 runs next

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.

k o t โ‡„ k o t

Click the arrow - we'll compare the texts letter by letter.

Code
1first = input()
2second = input()
3if first < second:
4 print(first)
5else:
6 print(second)
Screen
โ–
1/2 ยท line 1 runs next

A remote for the robot ๐ŸŽฎ

Type a command ๐ŸŽฎ: lewo (left), prawo (right) or stop - and press Enter โŽ. And what happens with another word?

๐Ÿค–1command = input()
if command == "lewo"
3print("โฌ…๏ธ Turning left")
โฌ…๏ธ
elif command == "prawo"
5print("โžก๏ธ Turning right")
โžก๏ธ
elif command == "stop"
7print("๐Ÿ›‘ Standing still")
๐Ÿ›‘
elsein every other case
9print("โ“ I don't know that command")
โ“

Click the arrow - I'll walk the program's path, line by line.

Code
1command = input()
2if command == "lewo":
3 print("โฌ…๏ธ Turning left")
4elif command == "prawo":
5 print("โžก๏ธ Turning right")
6elif command == "stop":
7 print("๐Ÿ›‘ Standing still")
8else:
9 print("โ“ I don't know that command")
Screen
โ–
1/2 ยท line 1 runs next

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 use int(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.