字符串格式化
Python的字符串格式化有两种方式:百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。
1、百分号方式
%[(name)][flags][width].[precision]typecode
● (name) 可选,用于选择指定的key
● flags 可选,可供选择的值有:
● + 右对齐;正数前加正好,负数前加负号;
● - 左对齐;正数前无符号,负数前加负号;
● 空格 右对齐;正数前加空格,负数前加负号;
● 0 右对齐;正数前无符号,负数前加负号;用0填充空白处
● width 可选,占有宽度
● .precision 可选,小数点后保留的位数
● typecode 必选
● s,获取传入对象的__str__方法的返回值,并将其格式化到指定位置
● r,获取传入对象的__repr__方法的返回值,并将其格式化到指定位置
● c,整数:将数字转换成其unicode对应的值,10进制范围为 0 <= i <= 1114111(py27则只支持0-255);字符:将字符添加到指定位置
● o,将整数转换成 八 进制表示,并将其格式化到指定位置
● x,将整数转换成十六进制表示,并将其格式化到指定位置
● d,将整数、浮点数转换成 十 进制表示,并将其格式化到指定位置
● e,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(小写e)
● E,将整数、浮点数转换成科学计数法,并将其格式化到指定位置(大写E)
● f ,将整数、浮点数转换成浮点数表示,并将其格式化到指定位置(默认保留小数点后6位)
● F 同上
● g,自动调整将整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是e;)
● G,自动调整为整数、浮点数转换成 浮点型或科学计数法表示(超过6位数用科学计数法),并将其格式化到指定位置(如果是科学计数则是E;)
● % ,当字符串中存在格式化标志时,需要用%%表示一个百分号
注:Python中百分号格式化是不存在将整数转换成二进制表示的方式
常用格式化:
msg = "i am %s my hobbby is %s" %(‘william‘, ‘basketball‘) print(msg) 输出结果: i am william my hobbby is basketball msg = ‘i am %s ,age %s‘ %(‘william‘,18) print(msg) 输出结果: i am william ,age 18 msg = ‘i am %(name)s age %(age)d‘ % {‘name‘: ‘william‘, ‘age‘: 19} print(msg) 输出结果: i am william age 19 #打印浮点数 tpl = "percent %.2f" % 99.97623445463344 print(tpl) 输出结果: percent 99.98 #打印百分比 tpl = "percent %.2f %%" % 99.97623445463344 print(tpl) 输出结果: percent 99.98 % msg = ‘i am %(name)+60s my hobby is basketball.‘ % {‘name‘: ‘william‘} print(msg) 输出结果: i am william my hobby is basketball. msg = ‘i am \033[43;1m%(name)+60s\033[0m my hobby is basketball.‘ % {‘name‘: ‘william‘} print(msg) 输出结果: i am william my hobby is basketball. print(‘root‘, ‘x‘, ‘0‘, ‘0‘,sep=‘:‘) 输出结果: root:x:0:0
2、Format方式
表达格式:
[fill]align][sign][#][0][width][,][.precision][type]
常用格式化:
msg = ‘i am {},age {}, {}‘.format(‘zhurui‘, 24, ‘william‘) print(msg) 输出结果: i am zhurui,age 24, william msg = ‘i am {},age {}, {}‘.format(*[‘zhurui‘, 24, ‘william‘]) print(msg) 输出结果: i am zhurui,age 24, william msg = ‘i am {0}, age {1},really {0}‘.format(‘zhurui‘,24) print(msg) 输出结果: i am zhurui, age 24,really zhurui msg = ‘i am {0}, age {1},really {0}‘.format(*[‘zhurui‘,24]) print(msg) 输出结果: i am zhurui, age 24,really zhurui msg = ‘i am {name}, age {age},really {name}‘.format(name=‘william‘, age=24) print(msg) 输出结果: i am william, age 24,really william msg = ‘i am {name}, age {age},really {name}‘.format(**{‘name‘: ‘william‘, ‘age‘: 24}) #format如果用字典的话,前面加** print(msg) 输出结果: i am william, age 24,really william msg = ‘i am{0[0]}, age {0[1]}, really {0[2]}‘.format([1, 2, 3], [11, 22, 33]) print(msg) 输出结果: i am1, age 2, really 3 msg = ‘i am {:s}, age {:d}, money {:f}‘.format(‘william‘,24,89.3432) print(msg) 输出结果: i am william, age 24, money 89.343200 msg = ‘i am {:s},age {:d}‘.format(*[‘william‘, 24]) print(msg) 输出结果: i am william,age 24 msg = ‘i am {name:s},age {age:d}‘.format(**{‘name‘: ‘william‘,‘age‘: 24}) print(msg) 输出结果: i am william,age 24 msg = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) print(msg) 输出结果: numbers: 1111,17,15,f,F, 1587.623000% msg = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 输出结果: numbers: 1111,17,15,f,F, 1587.623000% msg = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) print(msg) 输出结果: numbers: 1111,17,15,f,F, 1500.000000% msg = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15) print(msg) 输出结果: numbers: 1111,17,15,f,F, 1500.000000%