python的print格式化输出,以及使用format来控制。
20210305
time.strftime("%Y%m%d%H%M%S", time.localtime())
時間格式化
20210206
https://www.runoob.com/w3cnote/python3-print-func-b.html
重點
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 %
單獨一個% 就表示%
https://blog.csdn.net/qq_19691995/article/details/84197252
** 字典作為關鍵字參數傳入
20201228
print(’%2d-%02d’%(3,1))中的2和02表示什么意思?
%2d : 輸出內容至少占兩位且右對齊,輸出內容不足兩位時在左側用空格補齊,大于等于兩位時正常輸出
%02d :輸出內容至少占兩位且右對齊,輸出內容不足兩位時在左側用0補齊,大于等于兩位時正常輸出
冒號是填充
f 是浮點型
2是小數點位數
點號 是小數
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,打印整數,浮點數。
>>> print ("He is %d years old" % (23)) # 整數 %d
He is 23 years old>>> print ("His height is %f m" % (1.73)) # 浮點數 %f
His height is 1.730000 m>>> print ("His height is %.2f m" %(1.73)) # 浮點數(指定保留小數點位數) %.2f
His height is 1.73 m>>> print ("His height is %.4f m" %(1.73)) # 浮點數(強制保留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)) # 通過位置傳遞,相當方便,可以重復,可以換位置。
18,TaoXiao,18>>> print('{name}: {age}'.format(age=24,name='TaoXiao')) # 通過關鍵字傳遞
TaoXiao: 24
- 1
- 2
- 3
- 4
- 5
- 6
format還有其他很多用法。可以點擊這里。
總結
以上是生活随笔為你收集整理的python的print格式化输出,以及使用format来控制。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch.range() 和 py
- 下一篇: 查错bug