formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
'''
Test Result:
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 didn't sing." 'So I said goodnight.'
'''
心得:
善用逗号,使得print语句变短,符合Python的编码风格
本文通过实例演示了Python中使用格式化字符串进行不同数据类型的打印操作。包括整数、字符串、布尔值及递归格式化的应用,展示了如何利用逗号简化长print语句。
167

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



