The f means Formatted string literals and it's new in Python 3.6.


A formatted string literal or f-string is a string literal that is prefixed with f or F. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.


Some examples of formatted string literals:

>>> name = "Fred"
>>> f"He said his name is {name}."
"He said his name is Fred."

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is Fred."

>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is Fred."

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
result: 12.35

>>> today = datetime(year=2023, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
January 27, 2023

>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
0x400
Answer from Pedro Lobito on Stack Overflow
Top answer
1 of 7
151

The f means Formatted string literals and it's new in Python 3.6.


A formatted string literal or f-string is a string literal that is prefixed with f or F. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.


Some examples of formatted string literals:

>>> name = "Fred"
>>> f"He said his name is {name}."
"He said his name is Fred."

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is Fred."

>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
"He said his name is Fred."

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}" # nested fields
result: 12.35

>>> today = datetime(year=2023, month=1, day=27)
>>> f"{today:%B %d, %Y}" # using date format specifier
January 27, 2023

>>> number = 1024
>>> f"{number:#0x}" # using integer format specifier
0x400
2 of 7
63

In Python 3.6, the f-string, formatted string literal, was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast.

Example:

agent_name = 'James Bond'
kill_count = 9


# old ways
print("%s has killed %d enemies" % (agent_name,kill_count))

print('{} has killed {} enemies'.format(agent_name,kill_count))
print('{name} has killed {kill} enemies'.format(name=agent_name,kill=kill_count))
    

# f-strings way
print(f'{agent_name} has killed {kill_count} enemies')

The f or F in front of strings tell Python to look at the values , expressions or instance inside {} and substitute them with the variables values or results if exists. The best thing about f-formatting is that you can do cool stuff in {}, e.g. {kill_count * 100}.

You can use it to debug using print e.g.

print(f'the {agent_name=}.')
# the agent_name='James Bond'

Formatting, such as zero-padding, float and percentage rounding is made easier:

print(f'{agent_name} shoot with {9/11 : .2f} or {9/11: .1%} accuracy')
# James Bond shoot with  0.82 or  81.8% accuracy 

Even cooler is the ability to nest and format. Example date


from datetime import datetime

lookup = {
    '1': 'st',
    '21': 'st',
    '31': 'st',
    '2': 'nd',
    '22': 'nd',
    '3': 'rd',
    '23': 'rd'
}

dato = datetime.now()

print(f"{dato: %B %-d{lookup.get(f'{dato:%-d}', 'th')} %Y}")

# April 23rd 2022

Pretty formatting is also easier

tax = 1234

print(f'{tax:,}') # separate 1k \w comma
# 1,234

print(f'{tax:,.2f}') # all two decimals 
# 1,234.00

print(f'{tax:~>8}') # pad left with ~ to fill eight characters or < other direction
# ~~~~1234

print(f'{tax:~^20}') # centre and pad
# ~~~~~~~~1234~~~~~~~~

The __format__ allows you to funk with this feature. Example


class Money:
    
    def __init__(self, value, currency='€'):
        self.currency = currency
        self.value = value
        
    def __repr__(self):
        return f'Money(value={self.value}, currency={self.currency})'
        
    def __format__(self, *_):
        
        return f"{self.currency}{float(self.value):.2f}"
        
        
tax = 12.3446
money = Money(tax, currency='$')

print(f'{money}')
# $12.34

print(money)
# Money(value=12.3446, currency=$)

There is much more. Readings:

  • PEP 498 Literal String Interpolation
  • Python String Formatting
🌐
GeeksforGeeks
geeksforgeeks.org › python › formatted-string-literals-f-strings-python
f-strings in Python - GeeksforGeeks
May 16, 2026 - Example 3: This example evaluates an expression directly inside an f-string. ... Physics = 78 Chemistry = 56 Biology = 85 print(f"Alex got total marks {Physics + Chemistry + Biology} out of 300")
Discussions

What's the purpose of f'strings?
F strings are just a nice way to insert variables into a template string. Yes, there are other ways - but f strings are usually the best. What way would you use instead? More on reddit.com
🌐 r/learnpython
91
157
January 28, 2024
Are f-strings good practice?

Yes, definitely.

- Without f-strings you need to either pass in multiple arguments to `print`, like you've done there, or remember to turn `age` to a string in order to be able to add it.

- If you're passing multiple arguments to print, it's much more difficult to apply Python's Formatting Mini-Language to numbers than using f-strings (e.g. `f"{amount:.2f}"`).

- F-strings are more readable than adding and passing multiple arguments. Just look at the amount of commas and plus signs in your second code, which are not necessary with f-strings.

You need Python 3.6+ to use f-strings. Make sure your code will be ran on those Python versions.

More on reddit.com
🌐 r/learnpython
121
346
March 28, 2020
what is the benefit of f_strings VS commas in python?
Because most of the time you are not passing the string to print but something else that does not do the same thing that print does with multiple arguments. More on reddit.com
🌐 r/learnprogramming
48
52
July 12, 2024
Despite f-string formatting being the newest way of writing formatted strings, why do lots of people still use the old style % formatting in Python?
🌐 r/Python
215
441
August 14, 2021
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and output — Python 3.15.0 documentation
Formatted string literals (also called f-strings for short) let you include the value of Python expressions inside a string by prefixing the string with f or F and writing expressions as {expression}. An optional format specifier can follow ...
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - Then, the function prints the result to the screen. Note how your f-string in the call to print() takes advantage of format specifiers to conveniently format the code’s output.
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
To format values in an f-string, ... functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value. A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals: ... You can perform Python operations inside the placeholders. ... price = 59 tax = 0.25 txt = f"The price is {price + (price * tax)} dollars" print(txt) Try it ...
🌐
freeCodeCamp
freecodecamp.org › news › python-f-strings-tutorial-how-to-use-f-strings-for-string-formatting
Python f-String Tutorial – String Formatting in Python Explained with Code Examples
September 14, 2021 - As f-Strings are evaluated at runtime, you might as well evaluate valid Python expressions on the fly. ▶ In the example below, num1 and num2 are two variables. To calculate their product, you may insert the expression num1 * num2 inside a set of curly braces. num1 = 83 num2 = 9 print(f"The product of {num1} and {num2} is {num1 * num2}.")
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - price = 19.98765 formatted_price = f"{price:.3f}" print(formatted_price) # Output: 19.988 · Beyond basic formatting, f-strings enable advanced operations such as inline calculations, function calls, and even conditional expressions. This versatility makes them suitable for a wide range of applications in Python ...
Find elsewhere
🌐
Medium
medium.com › gustavorsantos › f-string-the-easiest-way-to-print-in-python-82f9aca01855
F-string: the easiest way to print() in Python - gustavorsantos - Medium
December 8, 2022 - F-string: the easiest way to print() in Python Learn how to use the f-strings. The easiest way to print a message in Python is using f-strings. Present since version 3.6, the “f-string” came to …
🌐
Littlecolumns
littlecolumns.com › learn › python › f-strings
Formatting output with f strings - Python's Not (Just) For Unicorns
And if you think that’s amazing, maybe you should just skip the next section, because your life is about to change forever. When you’re using your variable, you can also add weird little modifiers to tell Python how you want the variable displayed. It’s mostly used with numbres - you can add commas to big numbers, round long decimal numbers, left- or right-align, or a hundred other things! years = 65000000 print(f'Tyrannosaurus rex roamed the earth {years:,} years ago') pi_value = 3.14159265 print(f'The value of pi is roughly {pi_value:.3f}') id_number = 3 print(f'Your ID number is {id_number:04}')
🌐
Mimo
mimo.org › glossary › python › formatted-strings
Python Formatted Strings / f-string formatting Guide
from datetime import datetime user_last_login = datetime(2024, 2, 16, 11, 38) print(f"Last login: {user_last_login:%B %d, %Y at %H:%M}") f-strings also support JSON-friendly representations using repr, which ensures that objects are formatted in a readable string form. ... f-strings can include more than variables and values. They can include expressions, function calls, and even conditional logic: ... Python's so-called format mini-language provides advanced control over string formatting.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use f-strings in Python | note.nkmk.me
May 18, 2023 - print(f'{a} and {b}') # 123 and abc print(F'{a} and {b}') # 123 and abc ... Like regular string literals, f-strings can use single quotes '', double quotes "", and triple quotes ''', """. Create a string in Python (single/double/triple quotes, str())
🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
the f-string to help delineate the spacing. That number after the ":"will cause that field to be · that number of characters wide. The first line in the print() statement using f-strings is an
🌐
Boot.dev
boot.dev › lessons › 9c216a9b-6467-4526-88f5-02678df98d77
Learn Python for Beginners: F-Strings in Python | Boot.dev
In Python, we can create strings that contain dynamic values with the f-string syntax. num_bananas = 10 bananas = f"You have {num_bananas} bananas" print(bananas) # You have 10 bananas
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
F-string is very useful when formatting numbers. Say we have a number, and we want to align it to the right. We can do that using the syntax above. In this case, we only need to add the width element. If we want to add six blank spaces and align our text to the right, we only need to add a width of eight. The default behavior is to align the text to the right. number = 20 >>> print(f'{number}') 20 >>> print(f'{number:8}') 20 >>> print(f'{number:>8}') 20
🌐
DEV Community
dev.to › tusharsadhwani › what-the-f-strings-391c
What the f-strings? - DEV Community
July 11, 2021 - They're called f-strings because of the syntax: you put an f before the string literal, like so: ... >>> name, birth_year = 'Tushar', 2000 >>> print(f'I am {name}, and I was born in {birth_year}') I am Tushar, and I was born in 2000 >>> import ...
🌐
DataCamp
datacamp.com › tutorial › python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - Unlike traditional string formatting methods, f-strings provide a more straightforward and readable way to embed Python expressions directly within string literals. Consider this common scenario where you need to format a simple greeting: name = "Alice" age = 25 # Using % operator (old style) print("My name is %s and I'm %d years old" % (name, age)) # Using str.format() print("My name is {} and I'm {} years old".format(name, age)) # Using f-string print(f"My name is {name} and I'm {age} years old")
🌐
Studymachinelearning
studymachinelearning.com › python-string-formatting-using-f-string
Python: String Formatting using f-string – Study Machine Learning
f-string with Python’ Dictionary – Pass the Python dictionary to print the data. In [6]: fruit = {'Banana':25,'Apple':50,'Orange':30} for f in fruit: print(f'{f} ==> ${fruit[f]}') Out[6]: Banana ==> $25 Apple ==> $50 Orange ==> $30 · f-string calling function – You can call the python ...
🌐
Javatpoint
javatpoint.com › f-string-in-python
F String in Python - Javatpoint
F String in Python - F String in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Career Karma
careerkarma.com › blog › python › python f strings: the ultimate guide
Python f Strings: The Ultimate Guide | Career Karma
December 1, 2023 - Suppose we wanted to perform a Python mathematical function in an f string. We could do so using this code: name = "Lindsay Ballantyne" print(f"Your name is {name}. On your next birthday, you will be {22 + 1}.")