← Console bridge: text in, text out

Numbers into text

Join a number onto text by turning it into text first with str.

Scores, prices, countdowns and receipts all mix words with numbers. Python will not guess how to join them, so you tell it.

Last lesson the program joined text it was given. Now it will join a number onto text, such as Score: 5.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
score = 5
print("Score: " + score)

Read the code. You know + joins text. What happens when one of the pieces is a number?

What happens when you run it?

RunRun it and compare the result with your prediction.

Run

Run it and read the message. Then change line 2 to print("Score: " + str(score)) and run it again.

Line What it does Why
score = 5 Stores the number 5 No quotes, so it is a number, not text
"Score: " + score Fails + joins text to text, or adds number to number. It will not mix them
str(score) Gives back the text "5" str makes a text copy of the number. score itself stays a number
lesson.py
score = 5
print("Score: " + score)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
score = 5
print("Score: " + str(score))
print("Score: " + str(score + 1))
print(score + score)
print(str(score) + str(score))

Adding numbers and joining text look the same, but they are different.

The str is there, but not in the right place.

Fill in what each line shows.

lineshows
print("Score: " + str(score))Score: 5
print("Score: " + str(score + 1))
print(score + score)
print(str(score) + str(score))

This should show Lives: 3, but stops with an error. Click the line that still has the problem.

ModifyChange the code on purpose and check the result.

Modify

Change the program so it also prints how many letters the name has.

Try it yourselfYour edits are saved in this browser.
Need a hint?

len(name) gives the number of letters. It is a number, so it needs str.

Print how many letters the name has, such as Ada has 3 letters.

    Run your program. This ticks when your code does all of the above.

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    Mix of the last two lessons: the answer is stored, a number is worked out from it, and the number is turned into text.

    lesson.py
    name = input("What is your name? ")
    count = len(name)
    print(name + " has " + str(count) + " letters.")
    
    The person types Maria. What does the program print?

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    A café sells cups of hot chocolate. Write a receipt: store the number of cups and the price, work out the total, and print a line such as 3 cups cost $12.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Work out the total first: total = cups * price. Then turn it into text in the print line.

    Another hint

    Each number joined onto text needs its own str().

    Print a receipt line that joins a count and a calculated total with text.

      Run your program. This ticks when your code does all of the above.

      For teachers

      Students learn why a number cannot be joined onto text with + and fix it with str. About 30 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Running the lesson

      1. Predict: show the failing program and vote. Many students expect it to work, and that is the point. Then ask what + was designed to join.
      2. Run: read the friendly error together, then fix it with str.
      3. Investigate: score + score and str(score) + str(score) look alike but give 10 and 55. This prepares for typed numbers next lesson.
      4. Make: students choose their own receipt. Ask each to point to every str in their program.

      The hard part (on purpose)

      Remembering which pieces are numbers. Ask which piece has no quotes and works out to a number.

      When students get stuck

      Stretch and support

      Stretch: Print a receipt with a count, a price each, a total and a tip.

      Support: Give the line with the numbers joined and ask which pieces need str.

      Where it leads

      Next lesson: what input gives back is text, even digits, so adding two typed numbers needs a conversion the other way.

      What each step is for

      Now you can
      Next lesson4. Text into numbersWhat input gives back is always text, so turn it into a number with int before calculating with it.