In Python 3.6 f-strings are introduced.

You can write like this

print (f"So, you're {age} old, {height} tall and {weight} heavy.")

For more information Refer: https://docs.python.org/3/whatsnew/3.6.html

Answer from Nithin R on Stack Overflow
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί inputoutput.html
7. Input and Output β€” Python 3.14.5 documentation
The example also prints percentage multiplied by 100, with 2 decimal places and followed by a percent sign (see Format specification mini-language for details). Finally, you can do all the string handling yourself by using string slicing and concatenation operations to create any layout you ...
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_string_formatting.asp
Python String Formatting
Before Python 3.6 we had to use the format() method. F-string allows you to format selected parts of a string.
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί string.html
string β€” Common string operations β€” Python 3.14.5 documentation
Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string. The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name' selects the named attribute using getattr(), while an expression of the form '[index]' does an index lookup using __getitem__(). Changed in version 3.1: The positional argument specifiers can be omitted for str.format(), so '{} {}'.format(a, b) is equivalent to '{0} {1}'.format(a, b).
🌐
Codingem
codingem.com β€Ί home β€Ί python string formatting: best practices (with examples)
Python String Formatting: Best Practices (with Examples)
January 26, 2023 - The whole point of the new string formatting approach was to get rid of the previously mentioned % operator and make string formatting easier. In Python 3, the string formatting happens with the str.format() method you can call on any string.
🌐
DigitalOcean
digitalocean.com β€Ί community β€Ί tutorials β€Ί how-to-use-string-formatters-in-python-3
How To Use String Formatters in Python 3 | DigitalOcean
August 20, 2021 - Formatters work by putting in one or more replacement fields or placeholders β€” defined by a pair of curly braces {} β€” into a string and calling the str.format() method. You’ll pass into the method the value you want to concatenate with the string. This value will be passed through in ...
🌐
Medium
medium.com β€Ί @chinthapooja733 β€Ί string-formatting-in-python-a-comprehensive-look-at-format-and-f-strings-4cf4fbe6a785
String Formatting in Python: A Comprehensive Look at %, format(), and F-Strings | by Chinthapooja | Medium
January 20, 2024 - String Formatting in Python: A Comprehensive Look at %, format(), and F-Strings % Operator: This operator is used to replace the placeholders in the string with the actual values. name = "pooja" age …
Find elsewhere
🌐
Bentley
cissandbox.bentley.edu β€Ί sandbox β€Ί wp-content β€Ί uploads β€Ί 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
Updated 2022 A Guide to Formatting with f-strings in Python
Percentage. Multiplies the number by 100 and displays in fixed ('f') format, ... The variable, variable, is enclosed in curly braces { }. When variable = 10, the f-string
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί how-to-format-a-string-in-python
How to Format a String in Python
August 10, 2021 - In the above example, we define the variables that we want to format into the strings. Then we create the string with the placeholders for the variables. Bear in mind that the placeholders should match the types of the corresponding variables. We can link the actual variables to the placeholders using the % symbol at the end of the statement. This is a technique that many Python programmers consider a breath of fresh air, since it makes things easier for you.
🌐
Python
peps.python.org β€Ί pep-3101
PEP 3101 – Advanced String Formatting | peps.python.org
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing β€˜%’ string formatting operator.
🌐
Code with C
codewithc.com β€Ί code with c β€Ί python tutorials β€Ί python panda 🐼 β€Ί string formatting in python: a comprehensive tutorial
String Formatting In Python: A Comprehensive Tutorial - Code With C
March 7, 2024 - The value of pi rounded to three decimal places is 3.142. 255 in binary is 11111111. 255 in hexadecimal is ff. 255 in octal is 377. |Left | Center | Right| |apple | apple | apple| My friend's name is Bob and he is 30 years old. ... The string formatting techniques demonstrated in this program are crucial for any Python ...
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
The new-style simple formatter calls by default the __format__() method of an object for its representation. If you just want to render the output of str(...) or repr(...) you can use the !s or !r conversion flags. In %-style you usually use %s for the string representation but there is %r for a repr(...) conversion. class Data(object): def __str__(self): return 'str' def __repr__(self): return 'repr' ... In Python 3 there exists an additional conversion flag that uses the output of repr(...) but uses ascii(...) instead.
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί python-string-format
Python String format() Tutorial | DataCamp
October 23, 2020 - Adjust the strings so they are lowercase. Finally, print the variables first_pos and second_pos. # Assign the substrings to the variables first_pos = wikipedia_article[3:19].lower() second_pos = wikipedia_article[21:44].lower() When we run the above code, it produces the following result: ... Try it for yourself. To learn more about positional formatting, please see this video from our course, Regular Expressions in Python.
🌐
Duke
fintechpython.pages.oit.duke.edu β€Ί jupyternotebooks β€Ί 1-Core Python β€Ί 09.a-FormattingStrings-OldStyle.html
11. Formatting Strings - Old Style β€” Programming for Financial Technology
So far, we’ve been using string concatenation and multiple arguments to print() to format strings. In this notebook, we will use printf style formatting to put values into strings with a variety of different formats. Python offers three different ways to format strings:
🌐
Davidvarghese
notes.davidvarghese.net β€Ί programming-languages β€Ί python β€Ί core-concepts β€Ί python-string-formatting
Python String Formatting
January 28, 2024 - name = 'David' print(f'{name:=^13}') > ====David==== # Debug Strings (3.8+) print(f'My name is {name=}') > My name is name='David' # Format Specifiers pi = 3.1415 print(f'{pi=:.2f}) > pi=3.14 Β· Python String Formatting Best Practices – Real Python
🌐
Instagram
instagram.com β€Ί popular β€Ί python-3-format-string
Python 3 Format String
April 4, 2026 - We cannot provide a description for this page right now
🌐
Real Python
realpython.com β€Ί python-string-formatting
Python String Formatting: Available Tools and Their Features – Real Python
December 2, 2024 - In this tutorial, you'll learn about the main tools for string formatting in Python, as well as their strengths and weaknesses. These tools include f-strings, the .format() method, and the modulo operator.
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'
🌐
SkillVertex
skillvertex.com β€Ί blog β€Ί python-string-formatting
Python String Formatting -
March 19, 2024 - # Define variables with different ... string print(formatted_string) ... The Format () method was developed with Python 3 for monitoring the complex string formatting more smoothly....
🌐
mkaz.blog
mkaz.blog β€Ί working-with-python β€Ί string-formatting
Python String Formatting: Complete Guide
This comprehensive guide covers everything you need to know about Python string formatting, from basic f-strings to advanced formatting techniques.