Python Coding Exercise #04: User Input and f-Strings
Part of the series Python Practice Exercise — part 4
Before you start: Know what a variable is · Know print() and input()
This post groups five exercises that all do the same job: ask the user for something with input(), then use an f-string to put that answer into a line of output.
Before you start: f-strings in 30 seconds
An f-string is a string with the letter f right before the opening quotation mark. Anything you put inside curly braces { } is replaced with its value:
name = "Paul"
print(f"Hello, {name}!") # Hello, Paul!
Two things to remember:
- The
fis what makes the braces work. Without it, Python prints the braces as plain text:
name = "Paul"
print("Hello, {name}!") # Hello, {name}!
- Inside a string,
\nmeans “start a new line”. It lets oneprint()produce several lines. (I cover this in the strings section of Data Types in Python.)
Also remember from Exercise #01 that input() always returns a string. Here that’s perfect, because we only want to insert the answers into text.
Exercise 1: Name twice
The question
Please write a program which asks for the user’s name and then prints it twice, on two consecutive lines.
Example of how the program should work:
What is your name? Paul
Paul
Paul
My Solution
name = input("What is your name? ")
print(f"{name}\n{name}")
How it works
input("What is your name? ")shows the question, waits for the user to type, and stores the answer in the variablename.- The f-string
f"{name}\n{name}"inserts the name, then\nstarts a new line, then inserts the name again. - One
print()call prints both lines.
Notice the space after the question mark inside the prompt: "What is your name? ". Without it, the user’s answer would be squashed against the question like this: What is your name?Paul. The space isn’t something the user types. It’s part of the prompt text.
You could get the same result with two separate print(name) lines. The f-string version just shows how \n works.
Exercise 2: Name and exclamation marks
The question
Please write a program which asks for the user’s name and then prints it out twice on a single line so that there is an exclamation mark at the beginning of the line, another between the two names and a third one at the end of the line.
What is your name? Paul
!Paul!Paul!
My Solution
name = input("What is your name? ")
print(f"!{name}!{name}!")
How it works
An f-string can mix fixed text and values in any order. Here the fixed text is just the three ! characters, and the values are the two {name} slots:
| Part of the f-string | What it prints |
|---|---|
! | ! |
{name} | Paul |
! | ! |
{name} | Paul |
! | ! |
Put together, that’s !Paul!Paul!. The same variable can appear as many times as you like.
Exercise 3: Name and address
The question
Please write a program which asks for the user’s name and address. The program should also print out the given information, as follows:
Given name: Steve
Family name: Sanders
Street address: 91 Station Road
City and postal code: London EC05 6AW
Steve Sanders
91 Station Road
London EC05 6AW
My Solution
given_name = input("Given name: ")
family_name = input("Family name: ")
street_address = input("Street address: ")
city_code = input("City and postal code: ")
print(f"{given_name} {family_name}\n{street_address}\n{city_code}")
How it works
The program asks four questions, and each answer goes into its own variable. Using descriptive names like given_name and street_address makes the code easy to read, since you can tell what each variable holds without looking back.
Then a single f-string builds the three output lines:
{given_name} {family_name}is the first line. The space between the two braces puts a space between the names.\n{street_address}starts a new line, then adds the street.\n{city_code}starts another new line, then adds the city and postal code.
The last question, “City and postal code”, is stored in one variable, because the exercise treats it as one piece of information.
Exercise 4: Fix the code: Utterances
The question
Here is a program which should ask for three utterances and print them out, like so. However, there is something wrong with the code. Please fix it.
The 1st part: hickory
The 2nd part: dickory
The 3rd part: dock
hickory-dickory-dock!
My Solution
part_1 = input("The 1st part: ")
part_2 = input("The 2nd part: ")
part_3 = input("The 3rd part: ")
print(f"{part_1}-{part_2}-{part_3}!")
How it works
Three input() calls store the three words, and one f-string joins them with hyphens and finishes with an exclamation mark. The - and ! are fixed text, and the three { } slots are filled by the variables.
In a “fix the code” exercise, the trick is to read the program slowly and compare what it does with what the sample output shows. With f-strings, two slips are especially common:
- Forgetting the
f.print("{part_1}-{part_2}-{part_3}!")prints{part_1}-{part_2}-{part_3}!exactly, braces and all. - Misspelling a variable name inside the braces. If you stored the answer in
part_1but wrote{part1}, Python stops with aNameError, because it has never heard ofpart1.
The variable names in the braces must match the names you used when storing the input, character for character.
Exercise 5: Story
The question
Please write a program which prints out the following story. The user gives a name and a year, which should be inserted into the printout.
Please type in a name: Mary
Please type in a year: 1572
Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents.
The story should change according to the input given by the user.
My Solution
name = input("Please type in a name: ")
year = input("Please type in a year: ")
print(f"{name} is a valiant knight, born in the year {year}. One morning {name} woke up to an awful racket: a dragon was approaching the village. Only {name} could save the village's residents.")
How it works
This is the same idea as the earlier exercises, just with longer text. The name is stored once and used three times in the story, and the year is used once. Change the input, and the whole story changes with it.
Two small details:
yearis stored as a string, becauseinput()always returns one. That’s fine here, since we’re only inserting it into text. You’d only needint(year)if you wanted to do maths with it, such as working out an age.- The story contains an apostrophe in
village's. That works because the f-string is wrapped in double quotes, so a single quote inside it is just a character.
A very long line is hard to read. Python lets you split a long f-string into several pieces inside the brackets of print(), and joins the pieces for you:
print(
f"{name} is a valiant knight, born in the year {year}. "
f"One morning {name} woke up to an awful racket: "
f"a dragon was approaching the village. "
f"Only {name} could save the village's residents."
)
It prints exactly the same story. Each piece needs its own f and a space at the end, so the words don’t run together.
Key takeaways
input()asks the user a question and returns their answer as a string.- Put a space at the end of the prompt (
"What is your name? ") so the answer doesn’t touch the question. - An f-string (
f"...") lets you place variables inside text with{ }. Forget thef, and the braces print as plain text. \ninside a string starts a new line, so oneprint()can print several lines.- The same variable can be used in an f-string as many times as you want.
- Descriptive variable names (
given_name,street_address) make longer programs much easier to follow.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: These exercises are taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.