补上:第19日学习字符串拼接笔记

字符串拼接

ins="i an %s ,is old %s"%("test",[1,2,3])#%s  可以接受任何值,%d  只能传入数字
print(ins)

ins="i an %s ,is old %d"%("test",18)
print(ins)

tp1="percent %.2f"%99.67890#浮点数,f前面的2表示保留2位小数,结果:99.68
print(tp1)

tp1="percent %.2s"%99.67890#浮点数,s前面的2表示保留几位,结果:99。多数使用截起到字符串
print(tp1)

tp1="percent %.2f%%"%99.67890#浮点数,输出的%的方法:2个%%
print(tp1)

ins="i an %(name)s ,is old %(age)d"%{"name":"test","age":18}#可以指定传值,在%和s之间加key值,在%和d之间加value值,
print(ins)

字符串格式化:百分号 format
1.百分号
ins="i an %-20s ,is old %d"%("test",18)#表示左对齐20个位置
print(ins)

# “- ”表示:左对齐
# “+”表示:右对齐

1.format

tp1="i am {},age {} ,{}".format("test",18,"teat1")#一一对应取值,元组
print(tp1)

tp1="i am {2},age {1} ,{0}".format("test",18,"teat1")#可以根据下标取值,元组
print(tp1)

tp1="i am {1},age {1} ,{0}".format("test",18,"teat1")#可以取值
print(tp1)

tp1="i am {name},age {age} ,{age}".format(name="test",age=18)#可以取值,字典
print(tp1)

tp1="i am {:s},age {:d} ,{:f}".format("tesr",18,18.222)#s接收字符串 d数字 f浮点数
print(tp1)

tp1="i am {name},age {age} ,{age}".format(**{"name":"test","age":18})#当传的值是字典时需要加2个**
print(tp1)

tp1="i am {2},age {1} ,{0}".format(*["test",18,"teat1"])#当传的值是字典时需要加一个*
print(tp1)

tp1="umber;{:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,15.3456)#b二进制,b八进制 ,
print(tp1)
 
原文地址:https://www.cnblogs.com/jianchixuexu/p/11518865.html