打印方法问题Python

Joa*_*nge 0 python string

在python中,第二个%表示什么?

print "%s" % ( i )
Run Code Online (Sandbox Code Playgroud)

小智 8

正如其他人所说,这是Python字符串格式化/插值运算符.它基本上相当于C中的sprintf,例如:

a = "%d bottles of %s on the wall" % (10, "beer")

相当于像

a = sprintf("%d bottles of %s on the wall", 10, "beer");

在C.每个都有a设置的结果"10 bottles of beer on the wall"

但请注意,在Python 3.0中不推荐使用此语法; 它的替换看起来像

a = "{0} bottles of {1} on the wall".format(10, "beer")

这是有效的,因为任何字符串文字都会被Python自动转换为str对象.


Ali*_*har 5

第二个%是字符串插值运算符.

链接到文档.