如何在 Python3 中像 printf 一样打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19457227/
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 like printf in Python3?
提问by Mike L
In Python 2 I used:
在 Python 2 中,我使用了:
print "a=%d,b=%d" % (f(x,n),g(x,n))
I've tried:
我试过了:
print("a=%d,b=%d") % (f(x,n),g(x,n))
采纳答案by Rob?
In Python2, print
was a keyword which introduced a statement:
在 Python2 中,print
是一个引入语句的关键字:
print "Hi"
In Python3, print
is a function which may be invoked:
在 Python3 中,print
是一个可以被调用的函数:
print ("Hi")
In both versions, %
is an operator which requires a string on the left-hand side and a value or a tuple of values or a mapping object (like dict
) on the right-hand side.
在这两个版本中,%
是一个运算符,它需要左侧的字符串和右侧的值或值元组或映射对象(如dict
)。
So, your line ought to look like this:
所以,你的行应该是这样的:
print("a=%d,b=%d" % (f(x,n),g(x,n)))
Also, the recommendation for Python3 and newer is to use {}
-style formatting instead of %
-style formatting:
此外,对于 Python3 和更新版本的建议是使用{}
-style 格式而不是%
-style 格式:
print('a={:d}, b={:d}'.format(f(x,n),g(x,n)))
Python 3.6 introduces yet another string-formatting paradigm: f-strings.
Python 3.6 引入了另一种字符串格式化范例:f-strings。
print(f'a={f(x,n):d}, b={g(x,n):d}')
回答by thefourtheye
回答by kindall
Because your %
is outside the print(...)
parentheses, you're trying to insert your variables into the resultof your print
call. print(...)
returns None
, so this won't work, and there's also the small matter of you already having printed your template by this time and time travel being prohibited by the laws of the universe we inhabit.
因为您%
在print(...)
括号之外,所以您试图将变量插入到调用结果中print
。print(...)
返回None
,所以这行不通,还有一个小问题,你已经打印了你的模板,时间旅行被我们居住的宇宙法则所禁止。
The whole thing you want to print, including the %
and its operand, needs to be insideyour print(...)
call, so that the string can be built before it is printed.
您要打印的整个内容,包括%
和它的操作数,都需要在您的print(...)
调用中,以便可以在打印之前构建字符串。
print( "a=%d,b=%d" % (f(x,n), g(x,n)) )
I have added a few extra spaces to make it clearer (though they are not necessary and generally not considered good style).
我添加了一些额外的空格以使其更清晰(尽管它们不是必需的并且通常不被认为是好的风格)。
回答by Donald Derek
Simple Example:
简单示例:
print("foo %d, bar %d" % (1,2))
print("foo %d, bar %d" % (1,2))
回答by stackoverflowuser2010
Simple printf() function from O'Reilly's Python Cookbook.
来自O'Reilly 的 Python Cookbook 的简单 printf() 函数。
import sys
def printf(format, *args):
sys.stdout.write(format % args)
Example output:
示例输出:
i = 7
pi = 3.14159265359
printf("hi there, i=%d, pi=%.2f\n", i, pi)
# hi there, i=7, pi=3.14
回答by User707
print("Name={}, balance={}".format(var-name, var-balance))
回答by Famory Toure
A simpler one.
一个更简单的。
def printf(format, *values):
print(format % values )
Then:
然后:
printf("Hello, this is my name %s and my age %d", "Martin", 20)
回答by kva1966
Python 3.6 introduced f-strings for inline interpolation. What's even nicer is it extended the syntax to also allow format specifiers with interpolation. Something I've been working on while I googled this (and came across this old question!):
Python 3.6 为内联插值引入了 f 字符串。更好的是它扩展了语法以允许带有插值的格式说明符。我在谷歌搜索时一直在做的事情(并遇到了这个老问题!):
print(f'{account:40s} ({ratio:3.2f}) -> AUD {splitAmount}')
PEP 498has the details. And... it sorted my pet peeve with format specifiers in other langs -- allows for specifiers that themselves can be expressions! Yay! See: Format Specifiers.
PEP 498有详细信息。并且...它用其他语言中的格式说明符整理了我的宠物 - 允许说明符本身可以是表达式!好极了!请参阅:格式说明符。
回答by Anatoliy
Other words printf absent in python... I'm surprised! Best code is
其他词 printf 在 python 中不存在......我很惊讶!最好的代码是
def printf(format, *args):
sys.stdout.write(format % args)
Because of this form allows not to print \n. All others no. That's why print is bad operator. And also you need write args in special form. There is no disadvantages in function above. It's a standard usual form of printf function.
由于这种形式允许不打印\n。所有其他人没有。这就是为什么打印是不好的操作符。而且您还需要以特殊形式编写 args。以上功能没有缺点。它是 printf 函数的标准常用形式。