%----format 格式化字符串---- 生成器---- 迭代器

%方式格式化字符串


顺序传参数

o转换8进制
x转换十六进制

tp1 = "i am %s" % "alex"
tp2 = "i am %s age %d" %("alex",18)


指定名称传入参数
tp3 = "i am %(name)s age %(age)d" % {"name": "alex","age":25}


保留小数点后几位
tp4 = "percent %.2f" % 99.97623
tp5= "i am %(pp).2f" %{"pp":123.425556}


如果出现占位符 ,测写 %% 打印时 %
tp6 = "i am %(pp).2f %% " % {"pp":123.4567}
print(tp6)
i am 123.46 %

 


format 方式格式化字符串


b转换二进制


s1 = "{name:s} {age:d} {name:s}".format(name='alex',age=25,)
s2 = "{:*^20s}".format('han')
print(s2)
********han*********

传列表
tp7 = "i am {0},age {1} really{0}" .format(*{"server","25","alex"})

 

传字典

tp8 = "i am {name},age {age},really {name}".format(**{"name":"han","age":18})


生成器
def func():
yield 1
yield 2
yield 3
yield 6

ret = func()
'''for i in ret:
print(i)'''
'''r1 = ret.__next__()
print(r1)
r2 = ret.__next__()
print(r2)
r3 = ret.__next__()
print(r3)'''

def myrange(arg):
start = 0
while True:
if start > arg:
return
yield start
start +=1

re = myrange(3)
r = re.__next__()
print(r)
r = re.__next__()
print(r)
r = re.__next__()
print(r)
r = re.__next__()
print(r)


for 循环内部执行__next__()迭代器

 

posted @ 2016-12-04 21:35  ༺༽秋水.无痕༼༻  阅读(466)  评论(0编辑  收藏  举报
……