Here are the docs about the "new" format syntax. An example would be:

"({:d} goals, ${:d})".format(self.goals, self.penalties)

If both goals and penalties are integers (i.e. their default format is ok), it could be shortened to:

"({} goals, ${})".format(self.goals, self.penalties)

And since the parameters are fields of self, there's also a way of doing it using a single argument twice (as @Burhan Khalid noted in the comments):

"({0.goals} goals, ${0.penalties})".format(self)

Explaining:

  • {} means just the next positional argument, with default format;
  • {0} means the argument with index 0, with default format;
  • {:d} is the next positional argument, with decimal integer format;
  • {0:d} is the argument with index 0, with decimal integer format.

There are many others things you can do when selecting an argument (using named arguments instead of positional ones, accessing fields, etc) and many format options as well (padding the number, using thousands separators, showing sign or not, etc). Some other examples:

"({goals} goals, ${penalties})".format(goals=2, penalties=4)
"({goals} goals, ${penalties})".format(**self.__dict__)

"first goal: {0.goal_list[0]}".format(self)
"second goal: {.goal_list[1]}".format(self)

"conversion rate: {:.2f}".format(self.goals / self.shots) # '0.20'
"conversion rate: {:.2%}".format(self.goals / self.shots) # '20.45%'
"conversion rate: {:.0%}".format(self.goals / self.shots) # '20%'

"self: {!s}".format(self) # 'Player: Bob'
"self: {!r}".format(self) # '<__main__.Player instance at 0x00BF7260>'

"games: {:>3}".format(player1.games)  # 'games: 123'
"games: {:>3}".format(player2.games)  # 'games:   4'
"games: {:0>3}".format(player2.games) # 'games: 004'

Note: As others pointed out, the new format does not supersede the former, both are available both in Python 3 and the newer versions of Python 2 as well. Some may say it's a matter of preference, but IMHO the newer is much more expressive than the older, and should be used whenever writing new code (unless it's targeting older environments, of course).

Answer from mgibsonbr on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ string-interpolation
Python String Interpolation
String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name.
Top answer
1 of 5
174

Here are the docs about the "new" format syntax. An example would be:

"({:d} goals, ${:d})".format(self.goals, self.penalties)

If both goals and penalties are integers (i.e. their default format is ok), it could be shortened to:

"({} goals, ${})".format(self.goals, self.penalties)

And since the parameters are fields of self, there's also a way of doing it using a single argument twice (as @Burhan Khalid noted in the comments):

"({0.goals} goals, ${0.penalties})".format(self)

Explaining:

  • {} means just the next positional argument, with default format;
  • {0} means the argument with index 0, with default format;
  • {:d} is the next positional argument, with decimal integer format;
  • {0:d} is the argument with index 0, with decimal integer format.

There are many others things you can do when selecting an argument (using named arguments instead of positional ones, accessing fields, etc) and many format options as well (padding the number, using thousands separators, showing sign or not, etc). Some other examples:

"({goals} goals, ${penalties})".format(goals=2, penalties=4)
"({goals} goals, ${penalties})".format(**self.__dict__)

"first goal: {0.goal_list[0]}".format(self)
"second goal: {.goal_list[1]}".format(self)

"conversion rate: {:.2f}".format(self.goals / self.shots) # '0.20'
"conversion rate: {:.2%}".format(self.goals / self.shots) # '20.45%'
"conversion rate: {:.0%}".format(self.goals / self.shots) # '20%'

"self: {!s}".format(self) # 'Player: Bob'
"self: {!r}".format(self) # '<__main__.Player instance at 0x00BF7260>'

"games: {:>3}".format(player1.games)  # 'games: 123'
"games: {:>3}".format(player2.games)  # 'games:   4'
"games: {:0>3}".format(player2.games) # 'games: 004'

Note: As others pointed out, the new format does not supersede the former, both are available both in Python 3 and the newer versions of Python 2 as well. Some may say it's a matter of preference, but IMHO the newer is much more expressive than the older, and should be used whenever writing new code (unless it's targeting older environments, of course).

2 of 5
68

Python 3.6 now supports shorthand literal string interpolation with PEP 498. For your use case, the new syntax is simply:

f"({self.goals} goals, ${self.penalties})"

This is similar to the previous .format standard, but lets one easily do things like:

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal('12.34567')
>>> f'result: {value:{width}.{precision}}'
'result:      12.35'
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-string-interpolation
Python String Interpolation: A Beginner's Guide | DataCamp
February 13, 2025 - As we have seen, f-strings and the .format() method are both useful methods for string interpolation. Let's look at a comparison of the pros and cons of each method. ... Below, you can find some troubleshooting methods and top tips for using Python string interpolation.
๐ŸŒ
Real Python
realpython.com โ€บ python-f-strings
Python's F-String for String Interpolation and Formatting โ€“ Real Python
November 30, 2024 - Python's f-strings provide a readable way to interpolate and format strings. They're readable, concise, and less prone to error than traditional string interpolation and formatting tools, such as the .format() method and the modulo operator ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-interpolation
Python String Interpolation - GeeksforGeeks
July 12, 2025 - Let's consider an example to understand it better, suppose you want to change the value of the string every time you print the string like you want to print "hello <name> welcome to geeks for geeks" where the <name> is the placeholder for the name of the user. Instead of creating a new string every time, string interpolation in Python can help you to change the placeholder with the name of the user dynamically.
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ python-string-interpolation
An Introduction to Python String Interpolation | Linode Docs
May 20, 2022 - During string interpolation a string literal is evaluated and if any placeholders are present, they are substituted by the indicated variable values. String interpolation helps reduce repetition in code and allows for dynamic string substitution.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ 3 must-know methods for python string interpolation
3 Must-Know Methods for Python String Interpolation | Towards Data Science
January 29, 2025 - The letter s is used along with the % operator to indicate the placeholder. This is an old method so we will not go in detail. However, it is still important to know because you are likely to encounter code that includes strings with this method.
Find elsewhere
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ string-interpolation-python
Python String Interpolation: 4 Methods (with Code)
November 10, 2023 - String interpolation is a powerful feature in Python that allows for the dynamic insertion of variables into strings. It enhances code readability, simplifies string formatting, and provides flexibility in generating output.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is python string interpolation?
What is Python String Interpolation? | Scaler Topics
April 12, 2024 - String interpolation in Python programming language can be defined as the method of inserting data into a placeholder in a string literal (here the term placeholder is defined as a variable to which any value or data can be assigned by the ...
๐ŸŒ
Real Python
realpython.com โ€บ python-string-interpolation
String Interpolation in Python: Exploring Available Tools โ€“ Real Python
July 5, 2024 - String interpolation allows you to create strings by inserting objects into specific places in a target string template. Python has several tools for string interpolation, including f-strings, the str.format() method, and the modulo operator (%).
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ 10 python string interpolation approaches
Python String Interpolation: Enhancing Code Readability
May 21, 2024 - The โ€˜%โ€™ operator is one of Pythonโ€™s oldest string interpolation methods. It allows us to substitute values into a string using placeholders represented by โ€˜%sโ€™ for strings, โ€˜%dโ€™ for integers, โ€˜%fโ€™ for floats, and so on.
๐ŸŒ
Written by Rahul
blogs.rahultgeorge.com โ€บ string-interpolation-in-python
3 best ways to Format Strings in Python - Rahul's Blog
January 24, 2024 - The str.format() style is preferred ... the string to interpolate separately and inject the values to the placeholders as keyworded arguments. I use these three techniques during coding depending on the scenario. I would love to hear if I have missed any other techniques or if you guys have any preferences. #technical-blogs#data-structures#algorithms#python#python-beginner#python3#tutorial#...
๐ŸŒ
Like Geeks
likegeeks.com โ€บ home โ€บ python โ€บ python string interpolation (make dynamic strings)
Python string interpolation (Make Dynamic Strings)
November 22, 2023 - In this tutorial, you'll learn about Python string interpolation and how string formatting works using str.format(), f-strings, template strings, and more.
๐ŸŒ
Real Python
realpython.com โ€บ python-string-formatting
Python String Formatting: Available Tools and Their Features โ€“ Real Python
December 2, 2024 - In this string, you insert the content of your name variable using a replacement field. When you run this last line of code, Python builds a final string, 'Hello, Bob!'. The insertion of name into the f-string is an interpolation.
๐ŸŒ
Medium
medium.com โ€บ data-science โ€บ 3-must-know-methods-for-python-string-interpolation-99e6d90b439c
3 Must-Know Methods for Python String Interpolation | by Soner Yฤฑldฤฑrฤฑm | TDS Archive | Medium
April 1, 2021 - 3 Must-Know Methods for Python String Interpolation How about that print statement? String interpolation is kind of embedding variables into strings. Unlike typing an entire plain string โ€ฆ
๐ŸŒ
Python Central
pythoncentral.io โ€บ python-string-interpolation-a-comprehensive-guide
Python String Interpolation: A Comprehensive Guide | Python Central
March 1, 2024 - This feature helps reduce repetition in code and is typically used when the elements in a string aren't known or change from time to time. Interpolating strings in Python involves the string temple and the value (or set of values) that belong ...
๐ŸŒ
Devcamp
basque.devcamp.com โ€บ pt-full-stack-development-javascript-python-react โ€บ guide โ€บ guide-modern-python-string-interpolation
Guide to Modern Python String Interpolation
Now I have this path inside of ... called C_Strings and then I have a file called interpolation. Now all I have to do is run that and it prints out. Hi Kristine ยท It's all working properly. Once again if you do not have Python 3 as your default then simply type Python3 just like ...