Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is an f string in Python

Understanding f-strings in Python

When you're learning programming, especially in Python, one of the things you'll often need to do is combine text with variables. A variable is like a container for storing data values. Imagine you have a box where you can put anything you want, and you can label it with a sticker. In programming, that box is a variable, and the label is the variable's name.

Now, let's say you want to create a message that includes the contents of several boxes. In Python, one of the most convenient and powerful ways to construct such messages is by using what's called an "f-string".

The Basics of f-strings

An f-string, short for "formatted string literal," is a string that has an f at the beginning and curly braces {} containing expressions that will be replaced with their values. It's like writing a letter and leaving blank spaces to fill in later with specific details. Here's a simple example:

name = "Alice"
message = f"Hello, {name}!"

In this code, name is a variable that holds the string "Alice". The f-string is "Hello, {name}!". When Python sees the {name} inside the f-string, it replaces it with the value of the name variable, so the output will be Hello, Alice!.

Why Use f-strings?

Before f-strings were introduced in Python 3.6, programmers had to use older methods like concatenation (gluing strings together) or the format method to combine strings with variables. These methods work but can be less intuitive and more cumbersome, especially for beginners.

f-strings are not only more readable but also more concise and faster in performance. They make your code look clean and straightforward, which is always a plus when you're learning and want to understand at a glance what your code is doing.

Inserting Variables

One of the most common uses of f-strings is to insert variables into strings. For example:

age = 30
height = 5.9
intro = f"I am {age} years old and {height} feet tall."

Here, the f-string intro includes two variables: age and height. The resulting string would be: I am 30 years old and 5.9 feet tall.

Expressions Inside f-strings

But f-strings can do more than just insert variable values. You can also put expressions inside the curly braces. An expression is like a mini-equation that Python can evaluate to get a value.

a = 5
b = 10
result = f"The sum of {a} and {b} is {a + b}."

The expression a + b is evaluated, and its result (15) is inserted into the string, so the output is: The sum of 5 and 10 is 15.

Formatting Numbers

f-strings also allow you to format numbers in various ways. For example, you might want to round a floating-point number to two decimal places or format a large number with commas.

pi = 3.14159265
money = 1234567.89
formatted_pi = f"Pi rounded to two decimal places: {pi:.2f}"
formatted_money = f"Amount with commas: {money:,.2f}"

The output will be:

Pi rounded to two decimal places: 3.14
Amount with commas: 1,234,567.89

The :.2f tells Python to format the number as a floating-point number with two decimal places. The :, tells Python to include commas as thousands separators.

Including Braces in f-strings

What if you want to include actual curly braces in your string? Since curly braces are used to identify where to replace expressions in f-strings, you need a way to tell Python that you want the braces to be part of the string itself. You can do this by doubling the braces:

product = "book"
price = 19.99
message = f"The price of one {{product}} is {price:.2f} dollars."

The output will be:

The price of one {product} is 19.99 dollars.

Multiline f-strings

f-strings can span multiple lines, which is useful when you want to create a long message:

name = "Bob"
hobby = "painting"
multiline_message = f"""
Hello {name},
I heard you enjoy {hobby}.
That's awesome!

This code will output:

Hello Bob,
I heard you enjoy painting.
That's awesome!

Notice how the f-string is enclosed in triple quotes """ to allow it to extend over several lines.

Debugging with f-strings

A neat feature introduced in Python 3.8 is the ability to use the = sign within the curly braces to print both the expression and its value, which can be incredibly helpful for debugging:

x = 10

This will output:


It's like having a note that tells you both the name of the box (the variable) and what's inside it (the value).


As you can see, f-strings are a versatile tool in Python that can make your life as a beginner programmer much easier. They help you create dynamic strings that include variables and expressions in a readable and efficient way. Just like a skilled craftsman has a favorite tool for every job, you'll find that f-strings become your go-to for string formatting in Python.

Remember that the key to learning programming is practice. Try creating your own f-strings with different variables, expressions, and formatting options. The more you use them, the more intuitive they'll become.

In the end, f-strings are not just a feature of the Python language; they're a bridge that connects your logic with the world, allowing you to communicate information in a way that's both human-friendly and machine-efficient. So go ahead, format some strings, and watch your Python skills flourish!