静态网站策划书,免费网站建设企业,房产cms系统哪个好,最近的军事新闻大事10条转载自#xff1a;http://www.cnblogs.com/kaituorensheng/p/5709970.htmlpython自2.6后#xff0c;新增了一种格式化字符串函数str.format()#xff0c;威力十足#xff0c;可以替换掉原来的%注#xff1a;以下操作版本是python2.7映射示例语法通过{} 和 : 替换 %通过位…转载自http://www.cnblogs.com/kaituorensheng/p/5709970.htmlpython自2.6后新增了一种格式化字符串函数str.format()威力十足可以替换掉原来的%注以下操作版本是python2.7映射示例语法通过{} 和 : 替换 %通过位置 {0} is {1}.format(jihite, 4 years old)jihite is 4 years old {0} is {1} {0}.format(jihite, 4 years old)jihite is 4 years old jihite通过format函数可以接受不限参数个数、不限顺序通过关键字 {name}:{age}.format(age4,namejihite)jihite:4 {name}:{age}.format(age4,namejihite,locateBeijing)jihite:4format括号内用给变量赋值通过对象属性 class Person:... def __init__(self, name, age):... self.name,self.age name, age... def __func__(self):... return This guy is {self.name}, is {self.age} old.format(selfself)... s Person(jihite, 4) s.__func__()This guy is jihite, is 4 old通过下标 {0[0]} is {0[1]} years old!.format([jihite, 4])jihite is 4 years old! {0} is {1} years old!.format(jihite, 4)jihite is 4 years old!其实就是通过位置格式限定符通过{} : 符号填充和对齐^分别表示居中、左对齐、右对齐后面带宽度 {:10}.format(jihite) jihite {:10}.format(jihite)jihite {:^10}.format(jihite) jihite 精度和类型f精度常和f一起使用 {:.2f}.format(3.1415)3.14 {:.4f}.format(3.1)3.1000进制转化 {:b}.format(10)1010 {:o}.format(10)12 {:d}.format(10)10 {:x}.format(10)a其中b o d x分别表示二、八、十、十六进制千位分隔符 {:,}.format(1000000)1,000,000 {:,}.format(100000.23433)100,000.23433 {:,}.format(abcedef)Traceback (most recent call last):File , line 1, in ValueError: Cannot specify , with s.尤其是其中的精度与类型用起来很方便