Python3 字符串格式化
字符串的格式化方法分為兩種,分別為占位符(%)和format方式。占位符方式在Python2.x中用的比較廣泛,隨着Python3.x的使用越來越廣,format方式使用的更加廣泛。
一 占位符(%)
%% 百分號標記
%c 字符及其ASCII碼
%s 字符串
%d 有符號整數(十進制)
%u 無符號整數(十進制)
%o 無符號整數(八進制)
%x 無符號整數(十六進制)
%X 無符號整數(十六進制大寫字符)
%e 浮點數字(科學計數法)
%E 浮點數字(科學計數法,用E代替e)
%f 浮點數字(用小數點符號)
%g 浮點數字(根據值的大小采用%e或%f)
%G 浮點數字(類似於%g)
%p 指針(用十六進制打印值的內存地址)
%n 存儲輸出字符的數量放進參數列表的下一個變量中
%d
實例(Python3.0+):
age = 29 print("my age is %d" %age) #my age is 29
%s
實例(Python3.0+):
name = "makes" print("my name is %s" %name) #my name is makes
%f
實例(Python3.0+):
print("%6.3f" % 2.3) #2.300 print("%f" %2.3) #2.300000
%%
實例(Python3.0+):
tp1="percent %.2f %%" % 89.1234566 print (tp1) #percent 89.12 %
%()type
實例(Python3.0+):
info="I am %(name)s , %(age)d years old ." %{"name":"Lucy","age":8} print(info) #I am Lucy , 8 years old
字符串格式控制%[(name)][flag][width][.][precision]type
name:可為空,數字(占位),命名(傳遞參數名,不能以數字開頭)以字典格式映射格式化,其為鍵名
flag:標記格式限定符號,包含+-#和0,+表示右對齊(會顯示正負號),-左對齊,前面默認為填充空格(即默認右對齊),0表示填充0,#表示八進制時前面補充0,16進制數填充0x,二進制填充0b
width:寬度(最短長度,包含小數點,小於width時會填充)
precision:小數點后的位數,與C相同
type:輸入格式類型,請看上面
info="I am %(name)-10s , %(age)d years old ." %{"name":"Lucy","age":8} print(info) #I am Lucy , 8 years old .
補充:
user='root' uid=0 gid=0 print(user,uid,gid,sep=":" ) #root:0:0
二 format方法
位置映射
實例(Python3.0+):
print("{}:{}".format('192.168.0.100',8888)) #192.168.0.100:8888
關鍵字映射
實例(Python3.0+):
print("{server}{1}:{0}".format(8888,'192.168.1.100',server='Web Server Info :')) #Web Server Info :192.168.1.100:8888
元素訪問
實例(Python3.0+):
print("{0[0]}.{0[1]}".format(('baidu','com')))
#baidu.com
填充對齊
- ^、<、>分別是居中、左對齊、右對齊
實例1(Python3.0+)
print("{0}*{1}={2:0>2}".format(3,2,2*3)) #3*2=06 print("{:*^30}".format('centered')) #***********centered***********
實例2(Python3.0+):九九乘法表
for i in range(1,10): a = 1 while a <= i: print("{0}*{1}={2:0>2}".format(a,i,a*i),end="\t") a +=1 print() """ 1*1=01 1*2=02 2*2=04 1*3=03 2*3=06 3*3=09 1*4=04 2*4=08 3*4=12 4*4=16 1*5=05 2*5=10 3*5=15 4*5=20 5*5=25 1*6=06 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=07 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=08 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=09 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 """
精度設置
實例(Python3.0+):
print("{:.3f}".format(2.1415)) #2.142 print("{:.10f}".format(3.1415)) #3.1415000000
更多使用:
print("{:,}".format(123456))#輸出1234,56 print("{a:w^8}".format(a="8"))#輸出www8wwww,填充w print("%.5f" %5)#輸出5.000000 print("%-7s3" %("python"))#輸出python 3 print("%.3e" %2016)#輸出2.016e+03,也可以寫大E print("%d %s" %(123456,"myblog"))#輸出123456 myblog print("%(what)s is %(year)d" % {"what":"this year","year":2016})#輸出this year is 2016 print("{0}{1}".format("hello","fun"))#輸出hellofun,這與CSharp的格式化字符(占位符)相似 print("{}{}{}".format("spkk",".","cn"))#輸出spkk.cn print("{a[0]}{a[1]}{a[2]}".format(a=["spkk",".","cn"]))#輸出spkk.cn print("{dict[host]}{dict[dot]}{dict[domain]}".format(dict={"host":"www","domain":"spkk.cn","dot":"."}))#輸出www.spkk.cn print("{a}{b}".format(a="python",b="3"))#輸出python3 print("{who} {doing} {0}".format("python",doing="like",who="I"))#輸出I like python print ('number:{1:d}:{2:.2f}:{3:e}:{4:%}'.format(*[1,2,3000,4,5,])) #輸出 number:2:3000.00:4.000000e+00:500.000000% print ('number:{num:o}:{num:0>.2f}:{num:b}:{num:x}:{num:X}'.format(num=15)) #輸出 number:17:15.00:1111:f:F
1 print("{:,}".format(123456))#輸出1234,56 2 3 print("{a:w^8}".format(a="8"))#輸出www8wwww,填充w 4 5 print("%.5f" %5)#輸出5.000000 6 7 print("%-7s3" %("python"))#輸出python 3 8 9 print("%.3e" %2016)#輸出2.016e+03,也可以寫大E 10 11 print("%d %s" %(123456,"myblog"))#輸出123456 myblog 12 13 print("%(what)s is %(year)d" % {"what":"this year","year":2016})#輸出this year is 2016 14 15 print("{0}{1}".format("hello","fun"))#輸出hellofun,這與CSharp的格式化字符(占位符)相似 16 17 print("{}{}{}".format("spkk",".","cn"))#輸出spkk.cn 18 19 print("{a[0]}{a[1]}{a[2]}".format(a=["spkk",".","cn"]))#輸出spkk.cn 20 21 print("{dict[host]}{dict[dot]}{dict[domain]}".format(dict={"host":"www","domain":"spkk.cn","dot":"."}))#輸出www.spkk.cn 22 23 print("{a}{b}".format(a="python",b="3"))#輸出python3 24 25 print("{who} {doing} {0}".format("python",doing="like",who="I"))#輸出I like python