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 OverflowVideos
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
You need to apply your formatting to the string, not to the return value of the print() function:
print("So, you're %r old, %r tall and %r heavy." % (
age, height, weight))
Note the position of the ) closing parentheses. If it helps you understand the difference, assign the result of the formatting operation to a variable first:
output = "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
print(output)
You may find it easier to use str.format(), or, if you can upgrade to Python 3.6 or newer, formatted string literals, aka f-strings.
Use f-strings if you just need to format something on the spot to print or create a string for other reasons, str.format() to store the template string for re-use and then interpolate values. Both make it easier to not get confused about where print() starts and ends and where the formatting takes place.
In both f-strings and str.format(), use !r after the field to get the repr() output, just like %r would:
print("So, you're {age!r} old, {height!r} tall and {weight!r} heavy.")
or with a template with positional slots:
template = "So, you're {!r} old, {!r} tall and {!r} heavy."
print(template.format(age, height, weight)
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 index0, with default format;{:d}is the next positional argument, with decimal integer format;{0:d}is the argument with index0, 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).
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'