Chapter 1 · Foundations
Text and input
Step 1 of 20 · Concept
Text is a str
By the end of this lesson you can build text from values and read what the user types.
name = "Ada"
print(name)
print(len(name))
This prints Ada, then 3.
- The quotes,
"..."or'...', mark where the text starts and ends. They are not part of it. len(text)counts the characters:len("Ada")is 3. A space counts too.- Text is a
strobject. It never changes, so the shorthand of lesson 3 may draw it in its name's box.
Step 2 of 20 · Concept
+ joins text, * repeats it
| you write | gives |
|---|---|
"Ada" + " " + "King" | "Ada King" |
"-" * 10 | "----------" |
str(7) | "7": the number as text |
+= adds to the end of a name's text: after line = "Hi" and line += "!", line is "Hi!". As with
numbers, += makes a new object and moves the arrow.
Step 3 of 20 · Quiz
What will it print?
Type exactly what this program prints.
title = "Table"
title += " " + str(7)
line = "=" * len(title)
print(line)
print(title)
print(line)
Step 4 of 20 · Demo
See it run
Run it and compare with your answer. Then change 7 to 12 and run it again: the lines grow with the title.
Step 5 of 20 · Concept
f-strings put values into text
name = "Ada"
price = 1.5
print(f"{name} pays {price * 2:.2f}")
An f-string has an f before its quote. Python works out each {...} and puts the value into the text: this
prints Ada pays 3.00. Any expression goes in the braces, and :.2f after it shows two decimals.
Step 6 of 20 · Quiz
Predict the label
Type exactly what this program prints.
item = "Tea"
price = 2.5
print(f"{item}: {price * 3:.2f}")
print(f"{price=}")
Step 7 of 20 · Demo
See it run
Run it and compare with your answer.
Step 8 of 20 · Exercise
Your turn: a price label
The names are bound. Print two lines, each with one f-string:
- the count,
x, the item,atand the price; Total:andcount * price, with two decimals.
The output must be exactly:
4 x pencil at 1.25
Total: 5.00
An f-string has f before its quote. Each name you want in the text goes in braces: {count}.
The total is an expression in braces, count * price, with :.2f after it, inside the braces.
print(f"Total: {count * price:.2f}")
Step 9 of 20 · Concept
input() returns text
age = input("Age: ")
input("Age: ")prints its prompt,Age:, and waits for a line.- When you press Enter, it gives back what you typed.
- It is always a
str. Type15, andagepoints at the text"15", not at a number.
Step 10 of 20 · Concept
Input given in advance
In this course, a block's input is written in its task, and Run sends it to the program. Python prints each prompt, but not the answer you would type. So the next output joins the prompt's line:
city = input("City: ")with the inputParisshowsCity:,- then
print("You live in", city)addsYou live in Paris: the line isCity: You live in Paris. - Two prompts share a line too:
Name: Age: Ada is 36.
Step 11 of 20 · Quiz
What does the output show?
The input is Ada. Type exactly what this program prints.
name = input("Name: ")
print("Hello, " + name)
Step 12 of 20 · Demo
See it run
The input, Ada, is given in advance. Run it and compare with your answer.
Step 13 of 20 · Concept
int() and float() turn text into numbers
| you write | gives |
|---|---|
int("15") + 1 | 16 |
float("2.5") * 2 | 5.0 |
int("abc") | ValueError: invalid literal for int() with base 10: 'abc' |
"15" + 1 stops with a TypeError. So turn the text into a number first: age = int(input("Age: ")).
Step 14 of 20 · Quiz
What does input() + 1 do?
The input is 15. What does this program do?
age = input("Age: ")
print(age + 1)
Step 15 of 20 · Demo
Text in, numbers out
The input is 15, then 2.5. Each answer is turned into a number at once, so the program can compute. Run it.
Step 16 of 20 · Try it
Try it: next year
The input is 15. Run it: it stops with TypeError: can only concatenate str (not "int") to str. No answer is
shown, so the traceback starts on the prompt's line.
Put int(...) around input("Age: "), so that age is a number. Then it prints Age: Next year: 16.
The brackets of int(...) go around the whole call: int(input("Age: ")).
Step 17 of 20 · Exercise
Your turn: a tip
The input is a bill, 42.50, and a percent, 20.
- Read the bill as a
float(promptBill:) and the percent as anint(promptPercent:). - The tip is the bill times the percent, divided by 100.
- Print the tip and the total, bill plus tip, both with two decimals.
Both prompts share the first line. The output must be exactly:
Bill: Percent: Tip: 8.50
Total: 51.00
input gives text. Turn each answer into a number before you compute. With the bill left as text, / stops with TypeError: unsupported operand type(s) for /: 'str' and 'int'.
Put float(...) around the bill's input(...) and int(...) around the percent's. The tip is bill * percent / 100. Each line is an f-string with :.2f.
percent = int(input("Percent: "))
Step 18 of 20 · Check yourself
Check yourself: a ticket
No shape this time.
The input is a name, Ada, and an age, 12. A ticket costs 2 for each year of age.
Read both, with the prompts Name: and Age: . Then print the ticket, its price with two decimals. The output
must be exactly:
Name: Age: Ticket for Ada, age 12: 24.00
Two inputs, and only the age becomes a number. Then one f-string makes the whole line.
Step 19 of 20 · Challenge · optional
Challenge: shout it
Optional.
The input is Ada. Change the program so that it shouts the greeting, in capitals:
Name: HELLO, ADA!
Text has tools of its own, called with a dot after it. name.upper() gives new text: name in capitals.
Call .upper() on name inside the braces. Lesson 9 explains these tools, called methods.
{name.upper()}
Step 20 of 20 · Recap
Recap
- Text is a
str:+joins it,*repeats it,lencounts its characters, andstr(7)is"7". f"{item}: {price * 3:.2f}"puts values into text;:.2fshows two decimals.input("Name: ")prints its prompt and gives back astr, always.int(...)andfloat(...)turn text into numbers, so you can compute with them.
Next: lesson 5 reads the errors you have met, such as TypeError and ValueError.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
title = "Table"
title += " " + str(7)
line = "=" * len(title)
print(line)
print(title)
print(line)
item = "Tea"
price = 2.5
print(f"{item}: {price * 3:.2f}")
print(f"{price=}")
item = "pencil"
count = 4
price = 1.25
# two lines, one f-string each
name = input("Name: ")
print("Hello, " + name)
age = int(input("Age: "))
price = float(input("Price: "))
print("In 3 years:", age + 3)
print("Two cost:", price * 2)
age = input("Age: ")
print("Next year:", age + 1)
# read the bill (a float) and the percent (an int)
# print the tip and the total, with two decimals
# write your program here
name = input("Name: ")
print(f"Hello, {name}!")