Reputation: 313
Hey I have a question concerning this
print ("So, you're %r old, %r tall and %r heavy.") % (
age, height, weight)
The line doesn't work in python 3.4 do anyone know how to fix this?
Upvotes: 27
Views: 111100
Reputation: 1121158
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)
Upvotes: 21
Reputation: 1079
you write:
print("So, you're %r old, %r tall and %r heavy.") % (age, height, weight)
when the correct is:
print("So, you're %r old, %r tall and %r heavy." % (age, height, weight))
besides that, you should think about switching to the "new" .format style which is more pythonic and doesn't requice type declaration. Started with Python 3.0 but is backported to 2.6+
print("So, you're {} old, {} tall and {} heavy.".format(age, height, weight))
#or for pinning(to skip the variable expanding if you want something
#specific to appear twice for example)
print("So, you're {0} old, {1} tall and {2} heavy and {1} tall again".format(age, height, weight))
or if you want only python 3.6+ formatting:
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
Upvotes: 13
Reputation: 939
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
Upvotes: 54
Reputation: 61
Easier way:
print ("So, you're ",age,"r old, ", height, " tall and ",weight," heavy." )
Upvotes: 2
Reputation: 5061
You have problem in your syntax, near ...) % (
age, height, weight)
.
You already close the print
brfore %
operator. that's why print
function will not carry the argument you are passing in it.
just do like this in your code,
print ("So, you're %r old, %r tall and %r heavy." % (
age, height, weight))
Upvotes: 2
Reputation: 11385
Even though I don't know which exception you get, you can maybe try to use the format function instead:
print ("So, you're {0} old, {1} tall and {2} heavy.".format(age, height, weight))
And as mentioned within the other answers, you obviously had some issue with your parentheses.
I will still leave my solution as a reference if you want to use format
.
Upvotes: 4