问题:如何在Python3中像printf一样打印?

在Python 2中,我使用了:

print "a=%d,b=%d" % (f(x,n),g(x,n))

我试过了:

print("a=%d,b=%d") % (f(x,n),g(x,n))

In Python 2 I used:

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))

回答 0

在Python2中,print是一个引入了以下语句的关键字:

print "Hi"

在Python3中,print是可以调用的函数:

print ("Hi")

在这两个版本中,%都是一个运算符,它在左侧需要一个字符串,在右侧需要一个值或一个值的元组或一个映射对象(如dict)。

因此,您的行应如下所示:

print("a=%d,b=%d" % (f(x,n),g(x,n)))

另外,对于Python3和更高版本,建议使用{}-style格式而不是%-style格式:

print('a={:d}, b={:d}'.format(f(x,n),g(x,n)))

Python 3.6引入了另一种字符串格式范例:f-strings

print(f'a={f(x,n):d}, b={g(x,n):d}')

In Python2, print was a keyword which introduced a statement:

print "Hi"

In Python3, print is a function which may be invoked:

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.

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:

print('a={:d}, b={:d}'.format(f(x,n),g(x,n)))

Python 3.6 introduces yet another string-formatting paradigm: f-strings.

print(f'a={f(x,n):d}, b={g(x,n):d}')

回答 1

最推荐的方法是使用format方法。在这里了解更多

a, b = 1, 2

print("a={0},b={1}".format(a, b))

The most recommended way to do is to use format method. Read more about it here

a, b = 1, 2

print("a={0},b={1}".format(a, b))

回答 2

来自O’Reilly的Python Cookbook的简单printf()函数。

import sys
def printf(format, *args):
    sys.stdout.write(format % args)

输出示例:

i = 7
pi = 3.14159265359
printf("hi there, i=%d, pi=%.2f\n", i, pi)
# hi there, i=7, pi=3.14

Simple printf() function from O’Reilly’s Python Cookbook.

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

回答 3

Python 3.6引入了用于内联插值的f字符串。更好的是,它扩展了语法,还允许使用插值的格式说明符。我在Google上搜索时一直在努力的工作(并遇到了这个老问题!):

print(f'{account:40s} ({ratio:3.2f}) -> AUD {splitAmount}')

PEP 498包含详细信息。而且…它用其他语言的格式说明符排序了我的烦恼-允许说明符本身可以是表达式!好极了!请参阅:格式说明符

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!):

print(f'{account:40s} ({ratio:3.2f}) -> AUD {splitAmount}')

PEP 498 has 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.


回答 4

简单的例子:

print("foo %d, bar %d" % (1,2))

Simple Example:

print("foo %d, bar %d" % (1,2))


回答 5

一个简单的。

def printf(format, *values):
    print(format % values )

然后:

printf("Hello, this is my name %s and my age %d", "Martin", 20)

A simpler one.

def printf(format, *values):
    print(format % values )

Then:

printf("Hello, this is my name %s and my age %d", "Martin", 20)

回答 6

因为您%print(...)括号之外,所以您试图将变量插入到调用结果printprint(...)返回None,所以这将不起作用,还有一个小问题,您已经在这个时间和时间旅行中打印了模板,这是我们所居住的宇宙定律所禁止的。

你想整个事情进行打印,包括%和它的操作数,需要为内部print(...)通话,从而使打印之前它的字符串可以建成。

print( "a=%d,b=%d" % (f(x,n), g(x,n)) )

我添加了一些额外的空格以使其更清晰(尽管它们不是必需的,通常也不认为是好的样式)。

Because your % is outside the print(...) parentheses, you’re trying to insert your variables into the result of 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.

The whole thing you want to print, including the % and its operand, needs to be inside your print(...) call, so that the string can be built before it is printed.

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).


回答 7

python中没有其他的printf单词…我很惊讶!最好的代码是

def printf(format, *args):
    sys.stdout.write(format % args)

由于这种形式不允许打印\ n。其他所有人都没有。这就是为什么打印不好的原因。而且,您还需要以特殊形式编写args。上面的功能没有缺点。这是printf函数的标准常用形式。

Other words printf absent in python… I’m surprised! Best code is

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.


回答 8

print("Name={}, balance={}".format(var-name, var-balance))
print("Name={}, balance={}".format(var-name, var-balance))

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。