🚀 Alguni Start learning

Unit 5: Words, Deeper

Pull text apart and rebuild it.

Unit 5 of 31 in Python for kids. Its 5 lessons are Letter by Letter, Cutting Slices, String Powers, Looping Over Letters and Word Wizard — below is everything each one explains, and a question or two from it to try.

Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🔡 Letter by Letter

Words are rows of letters

You can reach into a word the same way you reach into a list — square brackets and a number. Counting starts at 0, so the first letter is number 0.

Python

word = "code"
print(word[0])
print(word[2])

It prints

c
d

Counting backwards

A minus number counts from the end. -1 is the last letter — much tidier than working out the length first.

Python

word = "code"
print(word[-1])
print(word[-2])

It prints

e
d

Try it yourself

What does this print?

Python

word = "python"
print(word[1])

How do you get the LAST letter of any word, however long?

  • word[0]
  • word[1]
  • word[-1]
  • word[len(word)]

Answer them in the app

🍰 Cutting Slices

Taking a piece

Two numbers with a colon cuts a slice out. It starts at the first number and stops *before* the second — the same rule range uses.

Python

word = "dragon"
print(word[0:3])
print(word[3:6])

It prints

dra
gon

Leaving a number out

Miss out the first number and it starts at the beginning; miss out the second and it runs to the end.

Python

word = "dragon"
print(word[:3])
print(word[3:])

It prints

dra
gon

Try it yourself

What does this print?

Python

word = "python"
print(word[1:4])

What does this print?

Python

word = "birthday"
print(word[:5])

Answer them in the app

✨ String Powers

Words can do things

Strings carry their own little tools, used with a dot after the word. They hand back a new word — the original never changes.

Python

name = "ada"
print(name.upper())
print(name)

It prints

ADA
ada

Swapping and trimming

replace swaps one bit of text for another. strip removes stray spaces from the ends — invaluable when a person typed the text.

Python

print("I like cats".replace("cats", "dogs"))
print("  hi  ".strip())

It prints

I like dogs
hi

Breaking a sentence up

split chops a sentence into a list of words, cutting wherever it finds a space.

Python

words = "red green blue".split()
print(words)
print(len(words))

It prints

['red', 'green', 'blue']
3

Try it yourself

What does this print?

Python

print("SHOUT".lower())

After name = "ada" and name.upper(), what is in name?

  • "ADA"
  • "ada"
  • nothing
  • an error

Answer them in the app

🔄 Looping Over Letters

A word is loopable

A for loop walks through a word one letter at a time, exactly as it walks through a list.

Python

for letter in "cat":
    print(letter)

It prints

c
a
t

Is it in there?

in asks whether something appears inside a word or a list. It gives back True or False.

Python

print("z" in "pizza")
print("q" in "pizza")

It prints

True
False

Try it yourself

What does this print?

Python

count = 0
for letter in "banana":
    if letter == "a":
        count = count + 1
print(count)

What does this print?

Python

print("cat" in "concatenate")

Answer them in the app

🏆 Word Wizard

Try it yourself

What does this print?

Python

word = "wizard"
print(word[0].upper() + word[1:])

What does this print?

Python

print("hello"[::-1])

Answer them in the app