Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86867
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-12 22:31
文章分类

全部博文(26)

文章存档

2016年(26)

我的朋友

分类: Python/Ruby

2016-12-13 09:31:56

python字符串格式化有两种方式:百分号方式,format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存


第一种:百分号方式


常用的有以下几种形式:

  1.     s1 = "i am %s" % "yn"
  2.     s2 = "i am %s,my age is %d" % ("yn", 18) #带有顺序的格式化
  3.     s3 = "i am %(name)s,my age is %(age)d" % {"name": "yn", "age": 18} #传字典
  4.     s4 = "percent %.2f" % 87.76745623683        #保留小数位
  5.     s5 = "percent %(p).2f" % {"p": 87.12514663}
  6.     s6 = "percent %.2f %%" % 89.354254            #注意想要打印出%,必须有两个%%

  7.     print(s1)
  8.     print(s2)
  9.     print(s3)
  10.     print(s4)
  11.     print(s5)
  12.     print(s6)    
  13.     
  14.     
  15.     结果如下:
  16.     i am yn
  17.     i am yn,my age is 18
  18.     i am yn,my age is 18
  19.     percent 87.77
  20.     percent 87.13
  21.     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位)

常用的有以下几种形式:

  1.     s1 = "name {},my age is {},i am from {}".format("yn", 18, "China")
  2.     s2 = "name {},my age is {},i am from {}".format(*["yn", 18, "China"])

  3.     s3 = "name {0},my age is {1},i am from {0}".format("yn", 18, "China") #可以传多个,但是只生效2个
  4.     s4 = "name {0},my age is {1},i am from {0}".format(*["yn", 18, "China"]) #传列表

  5.     s5 = "name {name},my age is {age}".format(name="yn", age=18)
  6.     s6 = "name {name},my age is {age}".format(**{"name": "yn", "age": 18}) #传字典

  7.     s7 = "name {0[0]},age {0[2]},i am from {1[1]}".format(["yn", 28, 18], ["China", "America"])

  8.     s8 = "name is {:s},age {:d},balance {:.2f}".format("yn", 18, 888888.888) #传字符、数值、浮点(保留两位小数)
  9.     s9 = "name is {:s},age {:d},balance {:f}".format(*["yn", 18, 888888.888])
  10.     s10 = "name is {name:s},age {age:d},balance {balance:f}".format(name="yn", age=18, balance= 8.888)
  11.     s11 = "name is {name:s},age {age:d},balance {balance:f}".format(**{"name":"yn", "age":18, "balance": 8.888})

  12.     s12 = "{:b},{:o},{:d},{:x},{:X},{:%}".format(15, 15, 15, 15, 15, 15, 12)
  13.     s13 = "{0:b},{0:o},{0:d},{0:x},{0:X},{0:%}".format(12, 13, 14, 15, 16, 17, 18)#只会对第一个参数12生效
  14.     s14 = "{num:b},{num:o},{num:d},{num:x},{num:X},{num:%}".format(num=18)
  15.     
  16.     s15 = "{:*^50s}".format("yn") #结合用法来看
  17.     
  18.     print("s1:", s1)
  19.     print("s2:", s2)
  20.     print("s3:", s3)
  21.     print("s4:", s4)
  22.     print("s5", s5)
  23.     print("s6:", s6)
  24.     print("s7:", s7)
  25.     print("s8:", s8)
  26.     print("s9:", s9)
  27.     print("s10:",s10)
  28.     print("s11:",s11)
  29.     print("s12:",s12)
  30.     print("s13:",s13)
  31.     print("s14:",s14)
  32.     print("s15:",s15)
  33.     
  34.     结果如下:
  35.     
  36.     s1: name yn,my age is 18,i am from China
  37.     s2: name yn,my age is 18,i am from China
  38.     s3: name yn,my age is 18,i am from yn
  39.     s4: name yn,my age is 18,i am from yn
  40.     s5 name yn,my age is 18
  41.     s6: name yn,my age is 18
  42.     s7: name yn,age 18,i am from America
  43.     s8: name is yn,age 18,balance 888888.89
  44.     s9: name is yn,age 18,balance 888888.888000
  45.     s10: name is yn,age 18,balance 8.888000
  46.     s11: name is yn,age 18,balance 8.888000
  47.     s12: 1111,17,15,f,F,1500.000000%
  48.     s13: 1100,14,12,c,C,1200.000000%
  49.     s14: 10010,22,18,12,12,1800.000000%
  50.     s15: ************************yn************************
阅读(1777) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~