Python练习 | Python3中的格式化输入输出之print函数
博主github:https://github.com/MichaelBeechan
博主CSDN:https://blog.csdn.net/u011344545
Python2版本和Python3版本是有區別的???????? ?Note :?"()"這個非常重要
1、Python2版
num = 7.9
ptint "The number is %f" % num?
The number is 7.900000
2、Python3版
####字符串 %s
print("My name is %s" % ("Beechan"))
#Output:
My name is Beechan
####整型 %d %i
print("I am %d years old" % (26))
#Output:
I am 26 years old
注意到里面有個“%”符號,這里要對這個符號作特別說明:
%后面可以使用修飾符(若有,則只能使用以下)
%[(name)][flags][width][.precision]typecode
[(name)]:字典的鍵名
[flags]:“-” 表示左對齊,默認為右對齊 ?“+”表示包含數字符號,正數也會帶“+” ? ?"0"零填充
[width]:制定最小寬度的數字
[.]:用于按照精度分割字段的寬度
[precision]:指定要打印字符串中的最大字符個數,浮點數中小數點之后的位數,或者整數的最小位數
eg.
d = {'x' : 12, 'y' : 28.456972, 'z' : 87 }
print ("%(x)-10d %(y)0.3g" % d)
總結
以上是生活随笔為你收集整理的Python练习 | Python3中的格式化输入输出之print函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装 | Windows 10下基于An
- 下一篇: Python练习 | Python中的运