Python中使用.format方法替代格式化操作符%
2016-10-15 15:51
796 查看
Python中格式化字符串的方式有:
格式化操作符%,基本语法:%[转换标记][宽度[.精确度]]转换类型,其中转换标记和转换类型详见《Python基础教程第2版》3.3节,《改善Python程序的91个建议》建议28;
.format方法,基本语法:[[填充符]对齐方式][符号][#][0][宽度][,][.精确度][转换类型],其中填充符是除了“{}”外任
4000
意符号,对齐方式,符号,转换类型详见《91个建议》。
%常见用法:
1.直接格式化字符或数值;
>>>print "your score is %06.1f" % 9.5
2.用元组格式化;
>>>print "the %s of a circle with radius %f is %0.3f" % (itemname,radius,math.pi*radius*2)
3.用字典格式化;
>>>itemdict = {'itemname':'circumference','radius':'3','value':math.pi*radius*2}
>>>print "the %(itemname)s of a circle with radius %(radius)f is %(value)0.3f" % itemdict
.format常见用法:
1.使用符号;
>>>"The number {0:,} in hex is: {0:#x},the number {1} in oct is {1:#o}".format(4746,45)
2.使用名称;
>>>print "the max number is {max},the min number is {min},the average number is {average:0.3f}".format(max=189,min=12.6,average=23.5)
3.通过属性
>>>class Customer(object):
... def __init__(self,name,gender,phone):
... self.name = name
... self.gender = gender
... self.phone = phone
... def __str__(self):
... return 'Customer({self.name},{self.gender},{self.phone})'.format(self.self) #通过str函数返回结果
4.格式化元组的具体项;
>>>point = (1,3)
>>>'X:{0[0]};Y:{0[1]}'.format(point)
参考书籍:
1.《Python基础教程第2版》
2.《编写高质量代码改善Python程序的91个建议》
格式化操作符%,基本语法:%[转换标记][宽度[.精确度]]转换类型,其中转换标记和转换类型详见《Python基础教程第2版》3.3节,《改善Python程序的91个建议》建议28;
.format方法,基本语法:[[填充符]对齐方式][符号][#][0][宽度][,][.精确度][转换类型],其中填充符是除了“{}”外任
4000
意符号,对齐方式,符号,转换类型详见《91个建议》。
%常见用法:
1.直接格式化字符或数值;
>>>print "your score is %06.1f" % 9.5
2.用元组格式化;
>>>print "the %s of a circle with radius %f is %0.3f" % (itemname,radius,math.pi*radius*2)
3.用字典格式化;
>>>itemdict = {'itemname':'circumference','radius':'3','value':math.pi*radius*2}
>>>print "the %(itemname)s of a circle with radius %(radius)f is %(value)0.3f" % itemdict
.format常见用法:
1.使用符号;
>>>"The number {0:,} in hex is: {0:#x},the number {1} in oct is {1:#o}".format(4746,45)
2.使用名称;
>>>print "the max number is {max},the min number is {min},the average number is {average:0.3f}".format(max=189,min=12.6,average=23.5)
3.通过属性
>>>class Customer(object):
... def __init__(self,name,gender,phone):
... self.name = name
... self.gender = gender
... self.phone = phone
... def __str__(self):
... return 'Customer({self.name},{self.gender},{self.phone})'.format(self.self) #通过str函数返回结果
4.格式化元组的具体项;
>>>point = (1,3)
>>>'X:{0[0]};Y:{0[1]}'.format(point)
参考书籍:
1.《Python基础教程第2版》
2.《编写高质量代码改善Python程序的91个建议》
相关文章推荐
- python字符串常量和使用format()函数格式化
- python字符串格式化方法 format函数的使用
- Python中格式化format()方法总结
- Python中应该使用%还是format来格式化字符串
- Python之字符串格式化,"format"替代"%"
- JAVA中使用format()方法格式化数据(日期转换,货币)
- Python字符串格式化--format()方法
- Python 字符串 string.format() 格式化方法
- python中format()方法格式化字符串
- python format 格式化输出方法
- java 字符串格式化方法 String.format()的使用
- JAVA字符串格式化:String.format()方法的使用
- python字符串格式化方法 format函数的使用
- java字符串格式化:String.format()方法的使用
- java字符串格式化:String.format()方法的使用大全
- String.Format数字格式化的使用方法介绍
- Python中格式化format()方法详解
- SimpleDateFormat 使用方法和中英文时间格式化转换
- python的print格式化输出,以及使用format来控制。
- python字符串格式化方法 format函数的使用