hug2wisdom / learnpython

note the points of learn python
0 stars 0 forks source link

多段打印 #2

Open hug2wisdom opened 4 years ago

hug2wisdom commented 4 years ago

当需要多段打印的时候,我们可以使用,"""......""" 或者是 '''.....''' ,因为 print 默认会在输出的结果前添加一个新的 line ,所以我们可以用 \ ,取消掉这个默认的 line。

eg:

print("""\
you know i love u
happy birthday
hhh~~~
""")

the result:

you know i love u
happy birthday
hhh~~~

注意此时没有 print 带来的默认的 new line.

hug2wisdom commented 4 years ago

image 效果如图 note that the initial newline is not included

hug2wisdom commented 4 years ago

输出: 在:后传入一个整数, 可以保证该域至少有这么多的宽度。 用于美化表格时很有用。

>>> table = {'Google': 1, 'Runoob': 2, 'Taobao': 3}
>>> for name, number in table.items():
...     print('{0:10} ==> {1:10d}'.format(name, number))
... 
Google     ==>          1
Runoob     ==>          2
Taobao     ==>          3