python学习笔记 7

来源:互联网 发布:大象vs鳄鱼 知乎 编辑:程序博客网 时间:2024/04/29 14:56

这部分主要是关于格式化打印。

比如说

#双引号""和单引号''的意义是一样的,打印字符串时没有区别print ("We are %s"%"student")print ("We are %s"%'student')#可以通过乘号*,复制打印相同的字符print ("%"*10)val1 = "we"val2 = " "val3 = "are"val4 = " "val5 = "hero"print (val1 + val2 + val3 + val4)print (val5)#通过逗号,可以把字符串打在一起,中间自动会加空格print (val1 + val2 + val3 + val4, val5)

结果:

We are student
We are student
%%%%%%%%%%
we are
hero
we are  hero


以及格式化字符%r,%r打印的是对象本身,会将目标按照最接近本原的格式打印
参考书籍'''

format = "%r %r %r %r"print (format%(1,2,3,4))print (format%("one","two","three", "four"))print (format%(True, False, False, True))print (format%(format, format, format, format))print (format%("The U.S. Air Force will soon dispatch its most advanced fighter jet,", "the F-22 Raptor, to Europe in a show of solidarity", "with allies that have concerns about Russian actions in Ukraine,", "Air Force leaders said Monday."))
结果:
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'
'The U.S. Air Force will soon dispatch its most advanced fighter jet,' 'the F-22 Raptor, to Europe in a show of solidar
ty' 'with allies that have concerns about Russian actions in Ukraine,' 'Air Force leaders said Monday.'


另外,通过print (""" xxxx """")的3个双引号,可以包含多行字符串。



0 0