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