F-strings in Python, introduced in Python 3.6, provide a concise and readable way to embed expressions directly within string literals. To create an f-string, prefix the string with f or F and use curly braces {} to include variables, expressions, or function calls.

Basic Syntax

name = "Alice"
age = 30
print(f"My name is {name} and I'm {age} years old.")

Output: My name is Alice and I'm 30 years old.

Formatting Specifiers

Use a colon : inside the braces to apply formatting. Common format specifiers include:

  • :.2f → Format float to 2 decimal places

    pi = 3.14159
    print(f"Pi rounded to 2 decimals: {pi:.2f}")  # Output: Pi rounded to 2 decimals: 3.14
  • :05d → Format integer with leading zeros to width 5

    num = 42
    print(f"Zero-padded: {num:05d}")  # Output: Zero-padded: 00042

Alignment and Padding

  • :<width> → Left-align

  • :>width → Right-align

  • :^width → Center-align

text = "Python"
print(f"Left: '{text:<10}'")     # Output: Left: 'Python     '
print(f"Right: '{text:>10}'")    # Output: Right: '     Python'
print(f"Center: '{text:^10}'")   # Output: Center: '  Python  '

Advanced Features

  • Self-documenting expressions (Python 3.8+): Use =

    x = 10
    print(f"{x=}")  # Output: x=10
  • Expression evaluation: Embed any valid Python expression

    print(f"Sum: {2 + 3 * 4}")  # Output: Sum: 14

Key Benefits

  • Faster than % formatting and .format()

  • More readable and maintainable

  • Supports any expression, including method calls and comprehensions

For full details, refer to the Python documentation on f-strings or the fstring.help cheat sheet.

To answer your first question... .format just seems more sophisticated in many ways. An annoying thing about % is also how it can either take a variable or a tuple. You'd think the following would always work:

"Hello %s" % name

yet, if name happens to be (1, 2, 3), it will throw a TypeError. To guarantee that it always prints, you'd need to do

"Hello %s" % (name,)   # supply the single argument as a single-item tuple

which is just ugly. .format doesn't have those issues. Also in the second example you gave, the .format example is much cleaner looking.

Only use it for backwards compatibility with Python 2.5.


To answer your second question, string formatting happens at the same time as any other operation - when the string formatting expression is evaluated. And Python, not being a lazy language, evaluates expressions before calling functions, so the expression log.debug("some debug info: %s" % some_info) will first evaluate the string to, e.g. "some debug info: roflcopters are active", then that string will be passed to log.debug().

Answer from Claudiu on Stack Overflow
🌐
Plain English
python.plainenglish.io › mastering-60-python-f-strings-a-comprehensive-guide-to-efficient-string-formatting-3b52862c1c5d
Mastering 60 Python F-Strings: A Comprehensive Guide to Efficient String Formatting
March 26, 2024 - To create an f-string, prefix the string with the letter ‘f’ or ‘F’. Inside the string, you can include expressions or variables within curly braces {}. Python will evaluate the expressions and replace them with their respective values when the string is formatted.
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value. ... A placeholder can also include a modifier to format the value.
Discussions

python - String formatting: % vs. .format vs. f-string literal - Stack Overflow
There are various string formatting methods: Python More on stackoverflow.com
🌐 stackoverflow.com
is there any difference between using string.format() or an fstring?
Don't forget that f-strings haven't been around forever. It may be partly old habits, it may be not keeping up to date with features, they may still be wanting to target a minimum python version that didn't support f-strings. I'd tend to prefer to use f-strings, but I wouldn't crucify someone for using perfectly valid language constructs. More on reddit.com
🌐 r/Python
146
317
October 9, 2022
Why "f" strings?

That’s not useful. You use like so:

name = “Paul”
age = 12
print(f”{name} is {age} years old.”)

More on reddit.com
🌐 r/learnpython
125
133
January 2, 2020
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
58
95
February 23, 2023
🌐
Analytics Vidhya
analyticsvidhya.com › home › mastering f-strings in python: the ultimate guide to string formatting
Mastering f-strings in Python: The Ultimate Guide to String Formatting
March 13, 2024 - Flexibility: F-strings support various formatting options, allowing us to customize the output according to our requirements. To use the f-strings in Python, you prefix the string with ‘f’ or ‘F’ and place any variables or expressions inside curly braces {}. Here is a basic example:
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
More information can be found in the printf-style String Formatting section. open() returns a file object, and is most commonly used with two positional arguments and one keyword argument: open(filename, mode, encoding=None)
🌐
Fstring
fstring.help
fstring.help: Python f-string guide
Cheat sheet tables can be found at fstring.help/cheat thanks to Trey Hunner. Repository on Github, contributions welcome! If you prefer an interactive version, . f-strings are strings with an f in front of them: f"..." or f'...'. Inside the f-string, curly braces can be used to format values ...
🌐
W3Schools
w3schools.com › python › python_strings_format.asp
Python - Format Strings
To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations. ... A placeholder can contain variables, operations, functions, and modifiers to ...
Find elsewhere
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - Python f-strings offer a concise and efficient way to interpolate variables, objects, and expressions directly into strings. By prefixing a string with f or F, you can embed expressions within curly braces ({}), which are evaluated at runtime.
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
April 24, 2024 - In this tutorial, you'll learn how to use Python format specifiers within an f-string to allow you to neatly format a float to your required precision.
🌐
The Python Coding Stack
thepythoncodingstack.com › p › custom-f-string-format-specifiers-python
I Want My Own Fancy F-String Format Specifiers… Sure You Can
March 19, 2025 - *l} displays an initial for the first name followed by a dot and a space, and then the full last name: M. Johnson · So, how does Python know what to do with format specifiers? Indeed, how does it know how to perform any core operation with objects? ... Note: It's probably easier to create instance methods to control these bespoke string displays.
Top answer
1 of 16
994

To answer your first question... .format just seems more sophisticated in many ways. An annoying thing about % is also how it can either take a variable or a tuple. You'd think the following would always work:

"Hello %s" % name

yet, if name happens to be (1, 2, 3), it will throw a TypeError. To guarantee that it always prints, you'd need to do

"Hello %s" % (name,)   # supply the single argument as a single-item tuple

which is just ugly. .format doesn't have those issues. Also in the second example you gave, the .format example is much cleaner looking.

Only use it for backwards compatibility with Python 2.5.


To answer your second question, string formatting happens at the same time as any other operation - when the string formatting expression is evaluated. And Python, not being a lazy language, evaluates expressions before calling functions, so the expression log.debug("some debug info: %s" % some_info) will first evaluate the string to, e.g. "some debug info: roflcopters are active", then that string will be passed to log.debug().

2 of 16
320

Something that the modulo operator ( % ) can't do, afaik:

tu = (12,45,22222,103,6)
print '{0} {2} {1} {2} {3} {2} {4} {2}'.format(*tu)

result

12 22222 45 22222 103 22222 6 22222

Very useful.

Another point: format(), being a function, can be used as an argument in other functions:

li = [12,45,78,784,2,69,1254,4785,984]
print map('the number is {}'.format,li)   

print

from datetime import datetime,timedelta

once_upon_a_time = datetime(2010, 7, 1, 12, 0, 0)
delta = timedelta(days=13, hours=8,  minutes=20)

gen =(once_upon_a_time +x*delta for x in xrange(20))

print '\n'.join(map('{:%Y-%m-%d %H:%M:%S}'.format, gen))

Results in:

['the number is 12', 'the number is 45', 'the number is 78', 'the number is 784', 'the number is 2', 'the number is 69', 'the number is 1254', 'the number is 4785', 'the number is 984']

2010-07-01 12:00:00
2010-07-14 20:20:00
2010-07-28 04:40:00
2010-08-10 13:00:00
2010-08-23 21:20:00
2010-09-06 05:40:00
2010-09-19 14:00:00
2010-10-02 22:20:00
2010-10-16 06:40:00
2010-10-29 15:00:00
2010-11-11 23:20:00
2010-11-25 07:40:00
2010-12-08 16:00:00
2010-12-22 00:20:00
2011-01-04 08:40:00
2011-01-17 17:00:00
2011-01-31 01:20:00
2011-02-13 09:40:00
2011-02-26 18:00:00
2011-03-12 02:20:00
🌐
Python
peps.python.org › pep-0498
PEP 498 – Literal String Interpolation | peps.python.org
These include %-formatting [1], str.format() [2], and string.Template [3]. Each of these methods have their advantages, but in addition have disadvantages that make them cumbersome to use in practice. This PEP proposed to add a new string formatting mechanism: Literal String Interpolation. In this PEP, such strings will be referred to as “f-strings”, taken from the leading character used to denote such strings, and standing for “formatted strings”.
🌐
Littlecolumns
littlecolumns.com › learn › python › f-strings
Formatting output with f strings - Python's Not (Just) For Unicorns
They work like fill-in-the-blanks with our variables, and all you need to do is add a f at the beginning of the string! F-strings also let us do fun tricks like rounding and adding commas if we have long decimals or large numbers.
🌐
DataCamp
datacamp.com › tutorial › python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - F-strings are string literals prefixed with 'f' or 'F' that contain expressions inside curly braces {}. These expressions are evaluated at runtime and then formatted using the __format__ protocol. Unlike traditional string formatting methods, f-strings provide a more straightforward and readable ...
🌐
Medium
medium.com › @DahlitzF › pythons-f-strings-vs-str-e22995cefef6
Python’s f-strings vs. str()
December 26, 2018 - A few months ago I’ve seen a tweet from a Python learner with a code snippet containing f-strings. I asked, why she’s not using format() . She answered, that this is the new way of formatting strings. I was curious about it as I didn’t hear about it before.
🌐
Real Python
realpython.com › python-string-formatting
Python String Formatting: Available Tools and Their Features – Real Python
December 2, 2024 - F-strings are generally the most readable and efficient option for eager interpolation in Python. Python’s string formatting mini-language offers features like alignment, type conversion, and numeric formatting.
🌐
VPK Technologies
vpktechnologies.com › home › blog › how to format string with f-strings in python
How to Format String with f-Strings in Python
June 22, 2025 - In this blog post, we’ll explore the power and flexibility of f-strings in Python. F-strings, short for formatted string literals were introduced in Python 3.6 as a new way to format strings.
🌐
Fstring
fstring.help › cheat
Python f-string cheat sheet
Type f with precision .n displays n digits after the decimal point. Type g with precision .n displays n significant digits in scientific notation. Trailing zeros are not displayed. ... An empty type is synonymous with d for integers. These format specifications only work on integers (int). ... These format specifications work on strings ...
🌐
Data School
dataschool.io › how-to-use-f-strings-with-pandas
How to use Python's f-strings with pandas
June 7, 2025 - To make an f-string, you simply put an f in front of a string. By putting the name and age objects inside of curly braces, those objects are automatically substituted into the string.
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
They provide a better way to format strings and make debugging easier, too. ... Summary: Python f-strings, introduced in version 3.6, allow cleaner string formatting by embedding variables directly.