1.百分号字符串拼接
s="i am %s,i like playing basketball"%"alex" print(s) s="i am %s,i like playing %s"%("alex","basketball") #%s可以接受一切,%d只能接受数字 print(s) a="alex" b="basketball" s="i am %s,i like playing %s"%(a,b) #接受变量 print(s) #浮点数 x="%.3f"% 3.141592657 print(x) #打印百分比(%%) x="%.3f %%"% 3.141592657 print(x) #传字典 s="i am %(a)s,i like playing %(b)s" % {"a":"alex","b":"basketball"} print(s)
2.format 字符串格式化
#默认一一对应 s="i am {} years old,i like {},i have {} sister".format("4","5","6") print(s) #指定对应 s="i am {2} years old,i like {1},i have {0} sister".format("4","5","6") print(s) #字典形式 s="i am {age} years old,i like {num},i have {sister} sister".format(age="4",num="5",sister="6") print(s) #取列表中的元素 s="i am {0[1]} years old,i like {0[2]},i have {1[1]} sister".format([1,2,3],[4,5,6]) print(s)
原文地址:https://www.cnblogs.com/2018-1025/p/9917798.html
时间: 2024-10-09 06:42:10