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
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and output — Python 3.15.0 documentation
Often you’ll want more control over the formatting of your output than simply printing space-separated values. There are several ways to format output. To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values. >>> year = 2016 >>> event = 'Referendum' >>> f'Results of the {year} {event}' 'Results of the 2016 Referendum'
Discussions

What is the purpose of (f) outside a string within the print function?
It is essentially a more compact ... "format" function. For more details, see the section on Formatted string literals in the online documentation or the page on Literal String Interpolation in the PEP guide. ... They are called f-strings. You can read more on them here: https://realpython.com/python-f-strings/ In short they let you print out variables ... More on teamtreehouse.com
🌐 teamtreehouse.com
3
July 6, 2020
What's the f in the print statement do?
In the print statement there’s an f outside the quoted expression. What does it do? What does it mean · There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
February 16, 2023
Purpose of the F-string
It's a better alternative if you need to inject several variables into the string, like if you wanted to write: print(f’Movie ticket price: {movie_ticket_price}\n Movie time: {movie_time}\n Movie length: {movie_length}. Etc.’) Is imho easier to read and modify than this, where you need to open and close quotes at every string segment: print(`Movie ticket price:` + movie_ticket_price + `\nMovie time:` + movie_time + `\nMovie length:` + movie_length + `. Etc.’) And those 2 are still, imo, easier to read than % and .format() xD More on reddit.com
🌐 r/learnpython
59
95
February 23, 2023
What does the "f" stand for in an f string? Why not some other letter?
🌐 r/learnpython
90
198
November 27, 2021
🌐
EyeHunts
tutorial.eyehunts.com › home › print(f python) | python f print – example code
Python f print - Example code - Tutorial - By EyeHunts
April 28, 2022 - The f or F in front of strings ... the variables if exist. agent = 'James Bond' num = 9 # old ways print('{0} has {1} number '.format(agent, num)) # f-strings way print(f'{agent} has {num} number')...
🌐
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 …
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - In the second example, you create an f-string that embeds a list comprehension. The comprehension creates a new list of powers of 2. The expressions that you embed in an f-string are evaluated at runtime. Then, Python formats the result using the .__format__() special method under the hood. This method supports the string formatting protocol. This protocol underpins both the .format() method, which you already saw, and the built-in format() function:
🌐
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")
🌐
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 ...
Find elsewhere
🌐
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 - And it returns "Learn JavaScript!" ... that has a call to the choice function inside the curly braces. print(f"Hello Python, tell me what I should learn....
🌐
YouTube
youtube.com › watch
Print() and F-Strings in Python Explained ✔️ - YouTube
Hi everyone, in this video, I'm going to show you how to use f strings along with the print statement in Python. F-strings, also known as formatted string li...
Published: March 27, 2024
🌐
W3Schools
w3schools.com › python › ref_func_print.asp
Python print() Function
Python Overview Python Built-in ... Plan Python Interview Q&A Python Training ... The print() function prints the specified message to the screen, or other standard output device....
🌐
Career Karma
careerkarma.com › blog › python › python f strings: the ultimate guide
Python f Strings: The Ultimate Guide | Career Karma
December 1, 2023 - Let’s take a look at an example of an f string in action. Suppose we want to add the name “Lindsay Ballantyne” to a string. We could do so using this code: name = "Lindsay Ballantyne" print(f"Your name is {name}.")
🌐
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 following is an example using alignment for both a number and a string. The "|" is used in · 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
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - #!/usr/bin/python name = 'John Doe' occupation = 'gardener' age = 34 msg = f'''name: {name} age: {age} occupation: {occupation}''' print(msg) This example constructs a multiline string that includes several variables. Multiline f-strings are useful for generating formatted reports or messages. $ python main.py name: John Doe age: 34 occupation: gardener · You can call functions directly inside f-strings.
🌐
GeeksforGeeks
geeksforgeeks.org › python-output-formatting
Python - Output Formatting - GeeksforGeeks
Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio ... Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution.
Published: December 16, 2024
🌐
WsCube Tech
wscubetech.com › resources › python › print-function
Python Print Function: With Syntax & Examples
August 6, 2026 - Master the Python print() function: syntax, examples & parameters (sep, end, flush) explained simply. Practice with hands-on code in this free tutorial.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
What's the f in the print statement do? - Curriculum Help - The freeCodeCamp Forum
February 16, 2023 - Here’s a simple code: for x in range(100): print(f"{x} bottles of beer on the wall!") In the print statement there’s an f outside the quoted expression. What does it do? What does it mean? I am just starti ng with Python…
🌐
Tutorials
zframez.com › tutorials › chapter 4: mastering the print function in python
Chapter 4: Mastering the Print Function in Python - Tutorials
October 16, 2024 - In this example, the first `print` statement ends with a comma instead of a newline, so the second `print` statement continues on the same line. You can use any string for the `end` argument. ... You would have noticed that a space is added between the arguments in the print function. ... print ( "Learning", "Python", "Basics" ) #Output : Learning Python Basics Print ( "Learning", "Python", "Basics", sep="-") #Output: Learning-Python-Basics
🌐
Quora
quora.com › What-is-the-printf-operator-in-Python
What is the printf operator in Python? - Quora
Answer (1 of 5): There is no printf in Python. There is print, which is a built-in function, but it’s not an operator. If what you’re asking is how can you get the printf functionality in Python, that we can do. printf, which first appeared in the C programming language 50 years ago, allows ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-print-function
Python print() function - GeeksforGeeks
It is one of the most commonly used functions for producing readable output. Example 1: This example prints a list, tuple, and string together using the default separator and ending.
Published: March 7, 2026