目录
1.占位符
%d:整数
print("hello,I am %d " %7)
hello,I am 7
%c:字符,Unicode编码对应字符
print("hello,I am %c " %20154)
hello,I am 人
%s:字符串
print("hello,I am %s " %"people")
hello,I am people
%f:小数,默认小数点后六位
print("hello,I am %s " %7)
hello,I am 7.000000
2.format格式化函数
print("{}".format("hello"))
hello
print("{0} {1}".format("hello","sir"))
hello sir
print("hello {name} ".format(name="sir"))
hello sir
#字典
s={"name1":"candy","name2":"mary" }
print("{name1},{name2}".format(name="sir"))
candy,mary
#列表
l=["hello","sir"]
print("{0[0]},{0[1]"}.format(l))
hello,sir
#保留小数点后三位
print("{:.3f}".format(1.234567))
1.235
#带符号
print("{:-.3f}".format(1.234567))
-1.235
3. f 字符串
#将字符串放在{}中
name=‘Bob’
print(f'my name is {name}.')
my name is Bob.
#可以在{}中放入表达式
apple_price =10
number =20
print(f'I spend {apple_price*number} RMB')
I spend 200 RMB
#保留小数点后两位
apple_price =10.2
number =20.25
print(f'I spend {apple_price*number:.2f} RMB')
I spend 206.55 RMB