如何在 Python3 中打印格式化的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26862773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print formatted string in Python3?
提问by Soxty
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?
该行在 python 3.4 中不起作用有谁知道如何解决这个问题?
回答by Vishnu Upadhyay
You have problem in your syntax, near ...) % (
age, height, weight).
您的语法有问题,接近 ...) % (
age, height, weight).
You already close the printbrfore %operator. that's why printfunction will not carry the argument you are passing in it.
just do like this in your code,
您已经关闭了printbrfore%运算符。这就是为什么print函数不会携带您传入的参数的原因。在你的代码中这样做,
print ("So, you're %r old, %r tall and %r heavy." % (
age, height, weight))
回答by Martijn Pieters
You need to apply your formatting to the string, not to the return value of the print()function:
您需要将格式应用于字符串,而不是应用于print()函数的返回值:
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.
您可能会发现它更易于使用str.format(),或者,如果您可以升级到 Python 3.6 或更高版本,格式化的字符串文字,又名 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.
如果您只需要在现场格式化某些内容以打印或出于其他原因创建字符串,则使用 f-strings,str.format()以存储模板字符串以供重复使用,然后插入值。两者都可以更容易地避免对print()开始和结束的位置以及格式化发生的位置感到困惑。
In both f-stringsand str.format(), use !rafter the field to get the repr()output, just like %rwould:
在f-stringsand 中str.format(),!r在字段之后使用以获取repr()输出,就像%r:
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)
回答by oopbase
Even though I don't know which exception you get, you can maybe try to use the format function instead:
即使我不知道您得到哪个异常,您也可以尝试使用 format 函数:
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.
如果您想使用format.
回答by John Paraskevopoulos
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+
除此之外,您应该考虑切换到“新” .format 样式,它更像 pythonic 并且不需要类型声明。从 Python 3.0 开始,但向后移植到 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:
或者如果你只想要 python 3.6+ 格式:
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
回答by fnitty
Easier way:
更简单的方法:
print ("So, you're ",age,"r old, ", height, " tall and ",weight," heavy." )
回答by Nithin R
In Python 3.6 f-strings are introduced.
在 Python 3.6 中引入了 f-strings。
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

