Python · Lesson 2

Strings

Text in Python is called a string: a string of characters, one after another. Last lesson you stored them; today you join them, stretch them, shout them and measure them, and meet the sneakiest beginner trap in Python.

1. Text needs quotes

Quotes tell Python “this is text, not code”. Single or double both work; just close what you open.

food = "pizza"
feeling = 'happy'

print(food)
print("food")   # quotes: prints the word food, not the box

That last line is the classic slip: food opens the variable box, "food" is just the four letters f-o-o-d.

2. Joining strings with +

On strings, + means stick together (the fancy word is concatenation). Python glues exactly what you give it, so spaces are your job:

first = "Hallam"
second = "Coder"

print(first + second)         # HallamCoder, squashed!
print(first + " " + second)   # with a space glued between

3. Stretch, shout, whisper

Three tools you will use constantly:

word = "echo"

print(word * 3)        # * repeats a string
print(word.upper())     # SHOUT IT
print("HELLO".lower())  # whisper it

.upper() and .lower() are written with a dot after the string, and give back a changed copy: the original stays as it was.

4. Measuring with len()

len() counts the characters, spaces included:

print(len("cat"))        # 3
print(len("hello there"))  # 11: the space counts too

name = "Alexandra"
print("Your name has", len(name), "letters")

5. The "55" trap

Here is the sneakiest beginner trap in Python. input() always gives back a string, even when the user types a number. And + on strings joins:

number = input("Type a number: ")
print(number + number)   # surprise!

Type 5 and you get 55, because Python joined the string "5" to the string "5". To do real maths, convert the string into a number with int():

number = int(input("Type a number: "))
print(number + number)   # now it is 10
Remember it as a rule: keyboard gives strings; int() turns a string into a whole number you can do maths on. This one line of knowledge fixes half of all beginner bugs.

6. Quick check Part 1 of 2

Six quick questions. This is half of completing the module. The coding task below is the other half.

Which of these is a string?

What does "ha" * 3 give?

What does len("apple") give?

What does "5" + "5" give?

What does "hi".upper() give?

The user types 7 when your code runs answer = input("Number? "). What is in answer?

7. Coding task: The word machine Part 2 of 2

Write a real program below and press Check. Read a word, then shout it, stretch it and count it, using today's three tools. The robot tries different words, so the string tools have to do the work. Passing this is the other half of completing the module.

solution.py
Output

            

Ctrl+Enter runs your code · Tab inserts 4 spaces · Python loads the first time you press Run or Check (5-10 seconds), then it’s instant.

How the module is marked: the quick check is worth 50% and this coding task is worth 50%. Do both to reach 100%. Your teacher can see your score on the progress page.