码迷,mamicode.com
首页 > 编程语言 > 详细

2018/11/6(python)

时间:2018-11-06 20:56:17      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:3.1   style   lex   play   col   格式   pre   form   for   

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)

标签:3.1   style   lex   play   col   格式   pre   form   for   

原文地址:https://www.cnblogs.com/2018-1025/p/9917798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!