python字符串格式化
2020-12-13 04:12
标签:number title 符号 无符号数 进制 小数点 选择 col bbb Python的字符串格式化有两种方式:%格式符方式,format方式 %[(name)][flags][width].[precision]typecode 注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式 (name) 可选,用于选择指定的key 执行结果: flags 可选,可供选择的值有: width 可选,占有宽度 name占10位,+,右对齐,age占10位,-,左对齐 执行结果: 空格,右对齐 0,用0填充空白处 执行结果: .precision 可选,小数点后保留的位数 只保留两位小数 执行结果: typecode 必选 执行结果: 执行结果: 执行结果: 执行结果: 数字格式的定义以 ‘:‘ 号开始。碰到了‘: ‘字符就知道要定义一个数字的显示格式了。格式的定义顺序为 [[fill]align][sign][#][0][width][,][.precision][type] fill 【可选】空白处填充的字符 align 【可选】对齐方式(需配合width使用) width 【可选】格式化位所占宽度 执行结果: # 【可选】对于二进制、八进制、十六进制,如果加上#,会显示 0b/0o/0x,否则不显示 三种方法表示 执行结果: numbers: 1111,17,15,f,F, 1587.623000%,A , 【可选】为数字添加分隔符,如:1,000,000 .precision 【可选】小数位保留精度 执行结果: format常用格式化 执行结果: python字符串格式化 标签:number title 符号 无符号数 进制 小数点 选择 col bbb 原文地址:https://www.cnblogs.com/songdanlee/p/11105807.html%格式符
a = "%(name)s-----%(age)d "%{‘name‘:‘xx‘,‘age‘:20}
print(a)
xx-----20
b = "%(name)+10s————————%(age)-10d————————"%{‘name‘:‘xx‘,‘age‘:20}
print(b)
xx————————20 ————————
c = "------%(year) d******%(age)010d "%{‘year‘:2016,‘age‘:-20}
print(c)
------ 2016******-000000020
d = ‘--------%(p).2f‘%{‘p‘:1.23456}
d1 = ‘--------%(p)f‘%{‘p‘:1.23456}
print(d)
print(d1)
--------1.23
--------1.234560
e =‘***%c***%o***%x‘%(65,15,15)
print(e)
***A***17***f
f = ‘-----%(num)e------%(num)E‘%{‘num‘:1000000000}
print(f)
-----1.000000e+09------1.000000E+09
g = ‘-----%(num)g------%(num1)G‘%{‘num‘:1000000000,‘num1‘:100}
print(g)
-----1e+09------100
s = ‘aaa %‘
print(s)
s1 = ‘aaa %s %%‘%(‘bbb‘)
print(s1)
aaa %
aaa bbb %
format方式
s1 =‘---{:*^20s}----‘.format(‘welcome‘)
print(s1)
s2 =‘---{:*>20s}----‘.format(‘welcome‘)
print(s2)
s3 =‘---{:*
---******welcome*******----
---*************welcome----
---welcome*************----
a1 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{:c}".format(15, 15, 15, 15, 15, 15.87623,65)
a2 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%},{1:c}".format(15,65)
a3 = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%},{cc:c}".format(num=15,cc=65)
print(a1)
print(a2)
print(a3)
numbers: 1111,17,15,f,F, 1500.000000%,A
numbers: 1111,17,15,f,F, 1500.000000%,An = ‘---{:,d}----‘.format(10000000)
n1 = ‘---{:.2f}----‘.format(1.2345)
print(n)
print(n1)
---10,000,000----
---1.23----
tp1 = "i am {}, age {}, {}".format("seven", 18, ‘alex‘)
tp2 = "i am {}, age {}, {}".format(*["seven", 18, ‘alex‘])
tp3 = "i am {0}, age {1}, really {0}".format("seven", 18)
tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
tp9 = "i am {:s}, age {:d}".format(*["seven", 18])
tp10 = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tp11 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
print(tp1)
print(tp2)
print(tp3)
print(tp4)
print(tp5)
print(tp6)
print(tp7)
print(tp8)
print(tp9)
print(tp10)
print(tp11)
i am seven, age 18, alex
i am seven, age 18, alex
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am 1, age 2, really 3
i am seven, age 18, money 88888.100000
i am seven, age 18
i am seven, age 18
i am seven, age 18
下一篇:Windows服务简单实例