python字符串格式化有两种方式:百分号方式,format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存
第一种:百分号方式
-
s1 = "i am %s" % "yn"
-
s2 = "i am %s,my age is %d" % ("yn", 18) #带有顺序的格式化
-
s3 = "i am %(name)s,my age is %(age)d" % {"name": "yn", "age": 18} #传字典
-
s4 = "percent %.2f" % 87.76745623683 #保留小数位
-
s5 = "percent %(p).2f" % {"p": 87.12514663}
-
s6 = "percent %.2f %%" % 89.354254 #注意想要打印出%,必须有两个%%
-
-
print(s1)
-
print(s2)
-
print(s3)
-
print(s4)
-
print(s5)
-
print(s6)
-
-
-
结果如下:
-
i am yn
-
i am yn,my age is 18
-
i am yn,my age is 18
-
percent 87.77
-
percent 87.13
-
percent 89.35 %
第二种:format
(%,可以居中,填充字符,二进制等这几项format功能比第一种强大)
[[fill]align][sign][#][0][width][,][.precision][type]
fill 【可选】空白处填充的字符
align 【可选】对齐方式(需配合width使用)
<,内容左对齐
>,内容右对齐(默认)
=,内容右对齐,将符号放置在填充字符的左侧,且只对数字类型有效。 即使:符号+填充物+数字
^,内容居中
sign 【可选】有无符号数字
+,正号加正,负号加负;
-,正号不变,负号加负;
空格 ,正号空格,负号加负;
# 【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示
, 【可选】为数字添加分隔符,如:1,000,000
width【可选】格式化位所占宽度
.precision 【可选】小数位保留精度
type 【可选】格式化类型
传入” 字符串类型 “的参数
s,格式化字符串类型数据
空白,未指定类型,则默认是None,同s
传入“ 整数类型 ”的参数
b,将10进制整数自动转换成2进制表示然后格式化
c,将10进制整数自动转换为其对应的unicode字符
d,十进制整数
o,将10进制整数自动转换成8进制表示然后格式化;
x,将10进制整数自动转换成16进制表示然后格式化(小写x)
X,将10进制整数自动转换成16进制表示然后格式化(大写X)
传入“ 浮点型或小数类型 ”的参数
e, 转换为科学计数法(小写e)表示,然后格式化;
E, 转换为科学计数法(大写E)表示,然后格式化;
f , 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
F, 转换为浮点型(默认小数点后保留6位)表示,然后格式化;
g, 自动在e和f中切换
G, 自动在E和F中切换
%,显示百分比(默认显示小数点后6位)
-
s1 = "name {},my age is {},i am from {}".format("yn", 18, "China")
-
s2 = "name {},my age is {},i am from {}".format(*["yn", 18, "China"])
-
-
s3 = "name {0},my age is {1},i am from {0}".format("yn", 18, "China") #可以传多个,但是只生效2个
-
s4 = "name {0},my age is {1},i am from {0}".format(*["yn", 18, "China"]) #传列表
-
-
s5 = "name {name},my age is {age}".format(name="yn", age=18)
-
s6 = "name {name},my age is {age}".format(**{"name": "yn", "age": 18}) #传字典
-
-
s7 = "name {0[0]},age {0[2]},i am from {1[1]}".format(["yn", 28, 18], ["China", "America"])
-
-
s8 = "name is {:s},age {:d},balance {:.2f}".format("yn", 18, 888888.888) #传字符、数值、浮点(保留两位小数)
-
s9 = "name is {:s},age {:d},balance {:f}".format(*["yn", 18, 888888.888])
-
s10 = "name is {name:s},age {age:d},balance {balance:f}".format(name="yn", age=18, balance= 8.888)
-
s11 = "name is {name:s},age {age:d},balance {balance:f}".format(**{"name":"yn", "age":18, "balance": 8.888})
-
-
s12 = "{:b},{:o},{:d},{:x},{:X},{:%}".format(15, 15, 15, 15, 15, 15, 12)
-
s13 = "{0:b},{0:o},{0:d},{0:x},{0:X},{0:%}".format(12, 13, 14, 15, 16, 17, 18)#只会对第一个参数12生效
-
s14 = "{num:b},{num:o},{num:d},{num:x},{num:X},{num:%}".format(num=18)
-
-
s15 = "{:*^50s}".format("yn") #结合用法来看
-
-
print("s1:", s1)
-
print("s2:", s2)
-
print("s3:", s3)
-
print("s4:", s4)
-
print("s5", s5)
-
print("s6:", s6)
-
print("s7:", s7)
-
print("s8:", s8)
-
print("s9:", s9)
-
print("s10:",s10)
-
print("s11:",s11)
-
print("s12:",s12)
-
print("s13:",s13)
-
print("s14:",s14)
-
print("s15:",s15)
-
-
结果如下:
-
-
s1: name yn,my age is 18,i am from China
-
s2: name yn,my age is 18,i am from China
-
s3: name yn,my age is 18,i am from yn
-
s4: name yn,my age is 18,i am from yn
-
s5 name yn,my age is 18
-
s6: name yn,my age is 18
-
s7: name yn,age 18,i am from America
-
s8: name is yn,age 18,balance 888888.89
-
s9: name is yn,age 18,balance 888888.888000
-
s10: name is yn,age 18,balance 8.888000
-
s11: name is yn,age 18,balance 8.888000
-
s12: 1111,17,15,f,F,1500.000000%
-
s13: 1100,14,12,c,C,1200.000000%
-
s14: 10010,22,18,12,12,1800.000000%
-
s15: ************************yn************************
阅读(1774) | 评论(0) | 转发(0) |