F-Strings: Making Python Output Clearer

One of the first challenges for new Python programmers is learning how to combine text, variables and numbers into useful output.

Students may begin with code like this:

hero = "Pixel"
score = 95

print(hero + " scored " + str(score) + " points!")

Open this example in Python Code Lab

This works, but it introduces several things at once. The student must manage spaces, quotation marks, plus symbols and the conversion of the number into text.

F-strings offer a much clearer alternative:

hero = "Pixel"
score = 95

print(f"{hero} scored {score} points!")

Open this example in Python Code Lab

The second version is easier to read because it looks much more like the final sentence. This is one of the main reasons f-strings are such a useful feature of modern Python.

Many older textbooks don’t mention this feature, because they were only added to Python in version 3.6 and so this elegant way of printing is often missed out. To make sure all students have access to it, we’ve created a course and added to the manual to ensure every student can take advantage of this feature!

What is an f-string?

An f-string is a formatted string literal. It begins with the letter f immediately before the opening quotation mark. Variables or expressions can then be placed inside curly brackets.

name = "Aisha"

print(f"Hello, {name}!")

Open this example in Python Code Lab

When Python runs this code, it evaluates whatever is inside the curly brackets and inserts the result into the string.

F-strings were introduced in Python 3.6. They allow the values of Python expressions to be placed directly inside strings.

Why are f-strings useful?

They make code easier to read

F-strings allow the code to follow the same structure as the message being produced.

Compare these two examples:

name = "Pixel"
coins = 12

print(name + " has collected " + str(coins) + " coins.")

Open the joined-string example in Python Code Lab

name = "Pixel"
coins = 12

print(f"{name} has collected {coins} coins.")

Open the f-string example in Python Code Lab

In the f-string version, it is immediately clear where the name and number will appear. There are fewer symbols for students to manage, making the purpose of the code easier to understand.

They work with different data types

Using the + operator to join text and numbers causes an error unless the number is first converted using str().

score = 50

print("Score: " + str(score))

Open this example in Python Code Lab

An f-string handles this conversion automatically:

score = 50

print(f"Score: {score}")

Open this example in Python Code Lab

This allows students to focus on the message they are creating rather than repeatedly converting values between data types.

They can perform calculations

The curly brackets in an f-string can contain expressions, not just variable names.

coins = 12
bonus = 5

print(f"You now have {coins + bonus} coins.")

Open this example in Python Code Lab

Python completes the calculation before inserting the result into the sentence.

This is particularly useful when students create score systems, bills, game statistics and reports.

price = 4
quantity = 3

print(f"Total cost: £{price * quantity}")

Open this example in Python Code Lab

They provide useful formatting

F-strings can control how numbers are displayed. This is helpful when working with money, measurements and percentages.

For example, :.2f displays a number using two decimal places:

total = 7.5

print(f"Total: £{total:.2f}")

Open this example in Python Code Lab

The output is:

Total: £7.50

Percentages can also be formatted directly:

wins = 3
games = 4
win_rate = wins / games

print(f"Win rate: {win_rate:.1%}")

Open this example in Python Code Lab

The output is:

Win rate: 75.0%

Python’s formatting system allows an optional format specifier to be placed after the expression. This gives the programmer control over decimal places, percentages, spacing and alignment.

They can use string methods

Students can also apply a string method inside the curly brackets:

hero = "nova"

print(f"Hero name: {hero.upper()}")

Open this example in Python Code Lab

This produces:

Hero name: NOVA

This is useful for titles, labels, usernames and character names.

For longer or more complicated operations, it is usually clearer to calculate the result first and store it in a separate variable.

Why are f-strings helpful for beginners?

F-strings connect several important introductory programming ideas:

  • Variables store information.
  • Input allows the user to provide information.
  • Calculations process information.
  • Output communicates the result.

Consider this short program:

name = input("What is your name? ")
coins = int(input("How many coins did you collect? "))

print(f"Well done, {name}! You collected {coins} coins.")

Open this example in Python Code Lab

Students can see a direct relationship between the information entered and the message displayed. Changing a variable produces an immediate and visible change in the output.

F-strings are also useful because they support creative tasks. Students can produce comic dialogue, game updates, football scores, shop receipts, character profiles and quiz results without needing advanced Python concepts.

Common f-string mistakes

Forgetting the letter f

The most common mistake is forgetting the letter f.

name = "Byte"

print("Hello, {name}!")

Open this incorrect example in Python Code Lab

This prints the curly brackets and the word name exactly as written.

The correct version is:

name = "Byte"

print(f"Hello, {name}!")

Open the corrected example in Python Code Lab

Using the wrong brackets

Students may use round brackets instead of curly brackets:

name = "Byte"

print(f"Hello, (name)!")

Open this incorrect example in Python Code Lab

Variables and expressions must be placed inside curly brackets:

name = "Byte"

print(f"Hello, {name}!")

Open the corrected example in Python Code Lab

Mixing quotation marks

Quotation marks can become confusing when accessing information from a dictionary. One solution is to use double quotation marks around the f-string and single quotation marks inside it:

user = {"name": "Pixel"}

print(f"Hello, {user['name']}!")

Open this example in Python Code Lab

Putting too much code inside an f-string

It is also important not to put too much code inside an f-string. A short calculation is usually clear, but complex logic should be completed before creating the output message.

The Ready Set Compute Python Code Guide highlights the missing f, quotation mark problems and overly complex expressions as important issues to check.

Learning through the F-String Comic Academy

The free F-String Comic Academy provides a ten-level beginner course built around comic-themed missions featuring Byte and Pixel.

Students begin by inserting simple text values into messages. They then progress through character announcements, gamer information, scores, bonus calculations, shop bills, issue titles and win-rate percentages. Later activities ask students to identify mistakes and create a more complete comic generator.

This progression matters. Students are not shown every formatting feature at once. Each mission adds a small new idea while continuing to practise the basic pattern:

variable = "value"

print(f"Text {variable} more text")

Open this pattern in Python Code Lab

The course is organised as a ten-step roadmap. Each step includes targeted hints and explanations, while the teacher portal provides teaching guidance and model Python solutions for classroom support.

The comic theme also gives the output a purpose. Instead of formatting disconnected example sentences, students create dialogue, powers, scores, prices and story details. This helps them see f-strings as a practical programming tool rather than an isolated piece of syntax.

Additional support in the Python Code Guide

The Ready Set Compute Python Code Guide provides a reference section for students who need further support.

It includes examples covering:

  • Multiple variables
  • Text and numbers
  • Calculations inside f-strings
  • String methods
  • Money with two decimal places
  • Percentage formatting
  • Common mistakes

The guide also includes an interactive f-string builder. Students can change values such as a hero name, number of coins and unit price, then see how the Python code and output change.

This makes the guide useful during lessons because students can check a pattern without needing the teacher to provide the complete answer to an activity.

A small feature with a big impact

F-strings are not one of Python’s largest or most complicated features, but they can significantly improve the quality of beginner code.

They reduce unnecessary conversions, make output statements easier to understand and allow calculations and formatting to be included naturally. Most importantly, they help students create programs that communicate clearly.

Once students understand the basic pattern, they can use it throughout almost every Python topic they study:

name = "Aisha"

print(f"Hello, {name}!")

Open this final example in Python Code Lab

From games and quizzes to reports and data processing, f-strings give students a simple way to turn stored values into meaningful output.

Author