Formatting text in Python — f-strings explained
In this lesson you will learn how to put variables into text in a simple and readable way using f-strings. F-strings are recommended for beginners because they are clear and easy to use.
What is an f-string?
An f-string is a formatted string that lets you put the values of variables directly into text.
You use it by putting the letter f or F in front of the string, and the variables go inside curly brackets { }.
An example of an f-string
name = "Mark"
age = 14
print(f"Hello, {name}! You are {age} years old.")
Result:
Hello, Mark! You are 14 years old.
Briefly about .format() (optional)
Before f-strings, Python used the .format() method to format text. Example:
name = "Ana"
age = 12
print("Hello, {}! You are {} years old.".format(name, age))
However, an f-string is more readable and more practical, especially for beginners.
Mini tasks
- Enter your name and print a message using an f-string.
- Enter your age and city, and print a personalised text.
- Make a short program that adds two numbers and prints the result inside a sentence.
F-strings make working with text in Python simple and clear. Once you master this technique, your programs will be more readable and more professional.
Advanced tasks with f-strings
Now we will practise f-strings combined with entering data and mathematical operations. These tasks are a bit more complex, but excellent for strengthening your logic and precise formatting of output.
Example 1: A personalised message with a decimal number
name = input("Enter a name: ")
height = float(input("Enter your height in cm: "))
print(f"Hi {name}! Your height is {height:.1f} cm.")
Here we use a float for the height and format the result to one decimal place with {height:.1f}.
Example 2: Calculating an average mark
student = input("Enter the student's name: ")
mark1 = int(input("First mark: "))
mark2 = int(input("Second mark: "))
mark3 = int(input("Third mark: "))
average = (mark1 + mark2 + mark3) / 3
print(f"{student}, your average is {average:.2f}")
We combine int input with an f-string for formatted output with two decimal places.
Example 3: A calculator with a text message
number1 = float(input("First number: "))
number2 = float(input("Second number: "))
total = number1 + number2
difference = number1 - number2
print(f"The sum of the numbers {number1} and {number2} is {total}")
print(f"The difference of the numbers {number1} and {number2} is {difference}")
Here we see how variables and the results of calculations can be combined in one sentence.
Mini advanced tasks for practice
- Write a program that asks the user for a name and three numbers, and then prints all the numbers and their sum in one sentence using an f-string.
- Write a program that reads the name and price of a product, then a discount in percent, and prints the price after the discount with two decimal places in an f-string.
- Write a program that reads a name, the year of birth and the current year, calculates the age and prints a personalised message: "{name} is {age} years old." using an f-string.
- Write a program that asks the user for the name of a product, the quantity and the price per piece, and then prints the total price in an elegant message using an f-string.
- Write a program that reads a person's name, height and weight and prints the message: "{name}, your BMI is {bmi:.2f}", where the BMI is calculated as weight / (height in metres)^2.
These tasks let you combine entering data, mathematical calculations and elegant output with f-strings. Practise and experiment with different formatting, decimal places and combinations of text and numerical values.
Hidden mini quiz — f-strings and formatting text
Click to check your knowledge of f-strings and formatting in Python!
Open the quiz
Mini quiz: Have you mastered f-strings?
1. How is an f-string written in Python?
2. What will be the result of the following code?
name = "Ana"
age = 12
print(f"{name} is {age} years old.")3. How do you format a decimal number to two decimal places in an f-string?
4. Which type of variable can you put into an f-string?
5. Which line correctly shows the result of a calculation in an f-string?
a = 5
b = 3
print(???)