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)