python的print格式化输出,以及使用format来控制。
20210305
time.strftime("%Y%m%d%H%M%S", time.localtime())
時(shí)間格式化
20210206
https://www.runoob.com/w3cnote/python3-print-func-b.html
重點(diǎn)
20210206
https://blog.csdn.net/a19990412/article/details/80149112
!r
20210205
https://www.jianshu.com/p/7fc0a177fd1f
%r
20210103
https://blog.csdn.net/weixin_42427638/article/details/80686294
print %
單獨(dú)一個(gè)% 就表示%
https://blog.csdn.net/qq_19691995/article/details/84197252
** 字典作為關(guān)鍵字參數(shù)傳入
20201228
print(’%2d-%02d’%(3,1))中的2和02表示什么意思?
%2d : 輸出內(nèi)容至少占兩位且右對(duì)齊,輸出內(nèi)容不足兩位時(shí)在左側(cè)用空格補(bǔ)齊,大于等于兩位時(shí)正常輸出
%02d :輸出內(nèi)容至少占兩位且右對(duì)齊,輸出內(nèi)容不足兩位時(shí)在左側(cè)用0補(bǔ)齊,大于等于兩位時(shí)正常輸出
冒號(hào)是填充
f 是浮點(diǎn)型
2是小數(shù)點(diǎn)位數(shù)
點(diǎn)號(hào) 是小數(shù)
https://blog.csdn.net/u012149181/article/details/78965472
1,打印字符串(str),利用%s。
>>> print ('My name is %s' % ('TaoXiao'))
My name is TaoXiao
- 1
- 2
- 3
2,打印整數(shù),浮點(diǎn)數(shù)。
>>> print ("He is %d years old" % (23)) # 整數(shù) %d
He is 23 years old>>> print ("His height is %f m" % (1.73)) # 浮點(diǎn)數(shù) %f
His height is 1.730000 m>>> print ("His height is %.2f m" %(1.73)) # 浮點(diǎn)數(shù)(指定保留小數(shù)點(diǎn)位數(shù)) %.2f
His height is 1.73 m>>> print ("His height is %.4f m" %(1.73)) # 浮點(diǎn)數(shù)(強(qiáng)制保留4位) %.4f
His height is 1.7300 m
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3,利用format。這是官方推薦用的方式,%方式將可能在后面的版本被淘汰。
>>> print('{1},{0},{1}'.format('TaoXiao',18)) # 通過(guò)位置傳遞,相當(dāng)方便,可以重復(fù),可以換位置。
18,TaoXiao,18>>> print('{name}: {age}'.format(age=24,name='TaoXiao')) # 通過(guò)關(guān)鍵字傳遞
TaoXiao: 24
- 1
- 2
- 3
- 4
- 5
- 6
format還有其他很多用法。可以點(diǎn)擊這里。
總結(jié)
以上是生活随笔為你收集整理的python的print格式化输出,以及使用format来控制。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pytorch.range() 和 py
- 下一篇: 查错bug