🌐
Programiz
programiz.com β€Ί python-programming β€Ί string-interpolation
Python String Interpolation
Strings in Python have a unique built-in operation that can be accessed with the % operator. Using % we can do simple string interpolation very easily. ... In above example we used two %s string format specifier and two strings Hello and World in parenthesis ().
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί python-string-interpolation
Python String Interpolation: A Beginner's Guide | DataCamp
February 13, 2025 - The text can be written across multiple lines, and it will appear in the output exactly as formatted. name = 'Mark' profession = 'Astronaut' age = 7 # This is an example of a multiline string bio = f""" Name: {name} Profession: {profession} Age: {age} """ print(bio)
Discussions

String formatting in Python 3 - Stack Overflow
I do this in Python 2: "(%d goals, $%d)" % (self.goals, self.penalties) What is the Python 3 version of this? I tried searching for examples online but I kept getting Python 2 versions. More on stackoverflow.com
🌐 stackoverflow.com
Is there a way to interpolate variables into a python string WITHOUT using the print function? - Stack Overflow
Every example I have seen of Python string variable interpolation uses the print function. More on stackoverflow.com
🌐 stackoverflow.com
Is there a Python equivalent to Ruby's string interpolation? - Stack Overflow
Ruby example: name = "Spongebob Squarepants" puts "Who lives in a Pineapple under the sea? \n#{name}." The successful Python string concatenation is seemingly verbose to me. More on stackoverflow.com
🌐 stackoverflow.com
Why do people use .format() method when f string literal exists?
f-strings exist for convenience. In doing so, they sacrifice portability, flexibility, and functionality. They are not back-wards compatible with python3.5, which is still floating around out there. They also have some drawbacks, like with the normal format method, you can re-use values, e.g. '{0}, {0}, and {1}'.format('ham', 'spam') Can't do that with f-strings. you can also build dynamically formatted strings: a = range(5) ( '{}'*len(a) ).format(*a) Which can be quite complex. there are probably more reasons others will comment about, but in general, f-strings are just for hard-coded generic messages that aren't anything special. the format function is still very necessary. More on reddit.com
🌐 r/learnpython
101
338
July 1, 2020
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-string-interpolation
Python String Interpolation - GeeksforGeeks
July 12, 2025 - The idea behind f-strings is to make string interpolation simpler. To create an f-string, prefix the string with the letter β€œ f ”. The string itself can be formatted in much the same way that you would with str. format(). F-strings provide a concise and convenient way to embed Python expressions inside string literals for formatting. Example: Formatting Strings using f-strings Β· Python3 Β·
🌐
Real Python
realpython.com β€Ί python-string-interpolation
String Interpolation in Python: Exploring Available Tools – Real Python
July 5, 2024 - Using f-strings, you can interpolate variables and expressions directly into your strings. Then, when Python executes the f-string, the variable’s content or the expression’s result will be interpolated into the f-string literal to build ...
🌐
Python
peps.python.org β€Ί pep-0498
PEP 498 – Literal String Interpolation | peps.python.org
Because the compiler must be involved in evaluating the expressions contained in the interpolated strings, there must be some way to denote to the compiler which strings should be evaluated. This PEP chose a leading 'f' character preceding the string literal.
🌐
Linode
linode.com β€Ί docs β€Ί guides β€Ί python-string-interpolation
An Introduction to Python String Interpolation | Linode Docs
May 20, 2022 - String substitutions are indicated using a $ interpolation character. The $ should be followed by the name of a dictionary key that has been passed as an argument to the Template class’s substitute() method. The substitute() method requires a dictionary-like object with keys as its argument. The Template() class accepts the template string as its argument. The example below imports the Template class, stores a new instance of the Template class and the string template in a variable.
🌐
Python
python.org β€Ί dev β€Ί peps β€Ί pep-0498
PEP 498 -- Literal String Interpolation | Python.org
August 1, 2015 - Because the compiler must be involved in evaluating the expressions contained in the interpolated strings, there must be some way to denote to the compiler which strings should be evaluated. This PEP chose a leading 'f' character preceding the string literal.
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'
Find elsewhere
🌐
Python Central
pythoncentral.io β€Ί python-string-interpolation-a-comprehensive-guide
Python String Interpolation: A Comprehensive Guide | Python Central
March 1, 2024 - You can then put a substitute function inside the class to replace the placeholder with the values. ... Using the f string is the most straightforward and simple way to interpolate strings.
🌐
Real Python
realpython.com β€Ί python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - The modulo operator (%) was the ... like in practice: ... In this quick example, you use the % operator to interpolate the value of your name variable into a string literal....
🌐
FavTutor
favtutor.com β€Ί blogs β€Ί string-interpolation-python
Python String Interpolation: 4 Methods (with Code)
November 10, 2023 - Your age is 20 Β· There are some ... want to provide in the placeholder. For example, if we want the placeholder to only be replaced by integer values we use the symbol %d....
🌐
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 - Here is an example. ... print(f"The house is in {location[:-2]} and is worth {price * 2}") The house is in Houston, and is worth 200000 Β· We omit the state and double the price. It is possible with the other methods as well.
🌐
Analytics Vidhya
analyticsvidhya.com β€Ί home β€Ί 10 python string interpolation approaches
Python String Interpolation: Enhancing Code Readability
May 21, 2024 - Introduced in Python 3.6, f-strings provide a concise and readable way to perform string interpolation. They allow us to embed expressions directly within curly braces {} using a prefix β€˜f’. Here’s an example:
🌐
Written by Rahul
blogs.rahultgeorge.com β€Ί string-interpolation-in-python
3 best ways to Format Strings in Python - Rahul's Blog
January 24, 2024 - Check out the example below. name = 'John' age = 25 print('My name is %s and I am %d years old.' % (name, age)) Use the below table as a guide for some of the common % symbols. The full list of symbols is available here.
🌐
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.
🌐
Scaler
scaler.com β€Ί home β€Ί topics β€Ί what is python string interpolation?
What is Python String Interpolation? | Scaler Topics
April 12, 2024 - ... In the preceding example, we have used 2 %s string format specifiers as well as 2 strings in () brackets, 'Welcome' and 'Hello'. As a result, the output we receive is "Welcome Hello".
🌐
Python Requests
python-requests.org β€Ί home β€Ί news β€Ί python string interpolation: a comprehensive guide with examples
Python String Interpolation: A Comprehensive Guide with Examples -
November 7, 2025 - This guide covers everything you need to know about Python string interpolation, with clear examples, comparisons, and best practices for writing clean, maintainable code.
🌐
Stack Abuse
stackabuse.com β€Ί python-string-interpolation-with-the-percent-operator
Python String Interpolation with the Percent (%) Operator
August 24, 2023 - The Python interpreter substitutes the first occurrence of %s in the string by the given string "one", and the second %s by the string "two". These %s strings are actually placeholders in our "template" string, and they indicate that strings will be placed there.