标签:需要 and format nbsp 打印 int span world 格式
s = ‘hello %s world‘ %‘wuli‘ print(s) #hello wuli world s = ‘hello %.2s world‘ %‘wuli‘ print(s) #hello wu world s = ‘my name is %s,i am %d years old‘ %(‘chen‘, 18) print(s) #my name is chen,i am 18 years old #打印浮点数 s = ‘My salary is %f‘ % 25333.5555555 print(s) #My salary is 25333.555555 s = ‘My salary is %.2f‘ % 25333.5555555 print(s) #My salary is 25333.56 #打印百分比 s = ‘percent is %.2f %%‘ % 99.032 print(s) #打印键值对 s = ‘My name is %(name)s, and I am %(age)s years old‘ %{‘name‘: ‘chen‘, ‘age‘: ‘18‘} print(s) #My name is chen, and I am 18 years old s = ‘My name is %(name)-20s, and I am %(age)s years old‘ %{‘name‘: ‘chen‘, ‘age‘: ‘18‘} print(s) #My name is chen , and I am 18 years old s = ‘My name is %(name)+20s, and I am %(age)s years old‘ %{‘name‘: ‘chen‘, ‘age‘: ‘18‘} print(s) #My name is chen, and I am 18 years old
s = ‘My name is {}, and I am {} years old‘ #必须一一对应 print(s.format(‘chen‘, ‘18‘)) #My name is chen, and I am 18 years old s = ‘My name is {1}, and I am {0} years old‘ #不需要一一对应 print(s.format(‘18‘, ‘chen‘,‘abc‘)) #My name is chen, and I am 18 years old s = ‘My name is {name}, and I am {age} years old‘ print(s.format(name = ‘chen‘, age = ‘18‘)) #My name is chen, and I am 18 years old s = ‘My name is {name}, and I am {age} years old‘ print(s.format(**{‘name‘: ‘chen‘, ‘age‘: ‘18‘})) #My name is chen, and I am 18 years old s = ‘My name is {0[1]}, and I am {1[1]} years old‘ print(s.format([0,1,2], [2,25])) #My name is 1, and I am 25 years old s = ‘My name is {:s}, and I am {:d} years old‘ print(s.format(‘chen‘, 18)) #My name is chen, and I am 18 years old s = ‘My name is {:s}, and I am {:d} years old‘ print(s.format(*[‘chen‘, 18])) #My name is chen, and I am 18 years old s = ‘My name is {name:s}, and I am {age:s} years old‘ print(s.format(name = ‘chen‘, age = ‘18‘)) #My name is chen, and I am 18 years old s = ‘My name is {:b}, and I am {:o} years old, {:d}, {:x}, {:X}, {:%}‘ #二进制,八进制,整型,小写十六进制,大写十六进制,百分比 print(s.format(15, 15, 15, 15, 15, 15.222222)) #My name is chen, and I am 18 years old
标签:需要 and format nbsp 打印 int span world 格式
原文地址:https://www.cnblogs.com/SakuraYuanYuan/p/10297844.html