python学习笔记2:print的使用中的一些问题

时间:2021-01-10 16:25:03

本篇博文,总结一些在程序设计过程中使用print遇到的一些问题


1、使用print输出多个重复字符,我们可以使用如下程序

print "." * 10

结果:..........


2、在print的结尾加入“,”可以续行,并产生空格

end1 = "C"
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"


print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12
结果:Cheese Burger


3、先看程序
print “%r %r %r %r” % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said good night."
)


结果:'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said good night.'
解释:使用“%r”,系统会根据需要输出双引号或者单引号,不要太在意


4、先看程序

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

结果:

There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.


解释:3引号中的内容会原样输出



原创性文章,转载请注明出处      
CSDN:http://blog.csdn.net/qingwufeiyang12346。