2018/11/6(python)
Posted hcy12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018/11/6(python)相关的知识,希望对你有一定的参考价值。
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)
以上是关于2018/11/6(python)的主要内容,如果未能解决你的问题,请参考以下文章