Python 印刷百分比
Python % in Printing
我这里有代码:
people = ['Amy','Tom','DeShawn','Catherine']
for i, f enumerate(people):
print("%r: The person's name is %s" %(i,f))
我的结果是
0: The person's name is Amy
1: The person's name is Tom
2: The person's name is DeShawn
3: The person's name is Catherine
谁能解释一下“%r”和“%s”在我的打印函数中是如何工作的。此外,是否可以使用任何其他值来替换 r 和 s 值。
格式的新样式使用format()
方法。其工作方式如下:
>>> people = ['Amy','Tom','DeShawn','Catherine']
>>> for i, f in enumerate(people):
... print ("{}: The person's name is {}".format(i, f))
有关详细信息,请参阅 https://docs.python.org/3/library/string.html#string-formatting
我这里有代码:
people = ['Amy','Tom','DeShawn','Catherine']
for i, f enumerate(people):
print("%r: The person's name is %s" %(i,f))
我的结果是
0: The person's name is Amy
1: The person's name is Tom
2: The person's name is DeShawn
3: The person's name is Catherine
谁能解释一下“%r”和“%s”在我的打印函数中是如何工作的。此外,是否可以使用任何其他值来替换 r 和 s 值。
格式的新样式使用format()
方法。其工作方式如下:
>>> people = ['Amy','Tom','DeShawn','Catherine']
>>> for i, f in enumerate(people):
... print ("{}: The person's name is {}".format(i, f))
有关详细信息,请参阅 https://docs.python.org/3/library/string.html#string-formatting