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.
🌐
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:
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
How to use f string format without changing content of string?
How to use f string format without changing content of string · It will lose a space character in second line. How can I prevent this behavior? I want to make a function to print the text above but the value in f string depend on the length of value current_money. More on discuss.python.org
🌐 discuss.python.org
0
June 15, 2022
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
Do you use f-strings?

No, but that's only because I try to support Python >=3.4 in most of the packages I maintain.

More on reddit.com
🌐 r/Python
32
20
June 15, 2015
🌐
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 ...
🌐
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.
🌐
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.
Find elsewhere
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”.
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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)
🌐
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.
🌐
MartinLwx's Blog
martinlwx.github.io › en › f-strings-in-python
The f-strings in Python3.6 - MartinLwx's Blog
April 19, 2024 - To create a f-string, we first add the leading f before "..." or '...'. ... Then, we can use {expression} inside the quotes. Python will evaluate this expression for us and print its corresponding value.
🌐
NetworkLessons
networklessons.com › home › python › python f-string formatting
Python F-string Formatting
April 1, 2022 - Python F-strings (available since Python 3.6) are the easiest option to format your strings. You can even run expressions.
🌐
Python.org
discuss.python.org › python help
How to use f string format without changing content of string? - Python Help - Discussions on Python.org
June 15, 2022 - def engine(current_money=10.0): # Instruction print( f""" --------------------------------------------------------------------------- | You start with ${current_money} …
🌐
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.