标签:ros 参数 eve div sans 分隔符 fill existing orm
Python的字符串格式化有两种方式: 百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101]
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing ‘%‘ string formatting operator.
1、百分号方式
1 %[(name)][flags][width].[precision]typecode
注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式
1 百分号方式格式化 2 1、 %s (str)万能的都可接受 3 a = "i am %s my hobby is %" %("lhf","gn") 4 print(a) 5 6 2、%d (isdigit)只能传数字int 例如 7 a = "i am %s my age is %d " %("lhf","gn") #报错 8 print(a) 9 10 a = "i am %s my age is %d " %("lhf",27) 11 print(a) 12 13 3、以键的形式去拼接字符串 后面传入的用字典的方式表现 14 a = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 15 print(a) 16 17 4、%f 打印浮点数 (float) a %f 默认保留六位小数 b .2代表保留两位小数 18 a = "percent %f" % 99.97623 19 b = "percent %.2f" % 99.97623 20 print(a) 21 print(b) 22 23 5、%% 打印百分比 24 a= "i am %(pp).2f %%" % {"pp": 123.425556, } 25 print(a) 26 27 print("root","x","0","0",sep=":")
2、Format方式
1 [[fill]align][sign][#][0][width][,][.precision][type]
tpl = "i am {}, age {}, {}".format("seven", 18, ‘alex‘) #按照默认索引值取值 tpl = "i am {}, age {}, {}".format(*["seven", 18, ‘alex‘]) # *号 遍历列表中的元素按索引赋值 tpl = "i am {0}, age {1}, really {0}".format("seven", 18) tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15
Python之路【第三篇】python基础 之基本数据类型 补充
标签:ros 参数 eve div sans 分隔符 fill existing orm
原文地址:http://www.cnblogs.com/sunkai1993/p/6116632.html