my_formatter = "%r %r %r %r"
print my_formatter % (1, 2, 3, 4)
print my_formatter % ("one", "two", "three", "four")
print my_formatter % (True, False, False, True)
print my_formatter % (my_formatter, my_formatter, my_formatter, my_formatter)
print my_formatter % (
"I had this thing.",
"That you could type up right.",
"But it don't sing.",
"So I said goodnight."
)
输出为:
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it don't sing." 'So I said goodnight.'
因为标红的部分已包含一个单引号,则输出用双引号括起来,其余都是用单引号
本文通过几个具体的示例展示了如何使用Python中的print函数进行字符串的格式化输出,包括整数、字符串、布尔值及多行文本的输出方式。

被折叠的 条评论
为什么被折叠?



