join用法与for循环用法

一:join用法

字符串迭代添加元素

s='dsfhhewfe'
s1='*'.join(s)
print(s1)

打印结果:

d*s*f*h*h*e*w*f*e

二:字符串循环(与for循环连用,可以添加break,continue)

1 for循环与break连用

for i in s:
    if i=='h':
        break
    print(i)

打印结果

2  for循环与continue连用

for i in s:
    if i=='h':
        continue
    print(i)

打印结果

三:while与else连用

while 1:
    pass
else:
    pass

四:for与else连用

s='alkshfdfhg'
for i in s:
    if i=='h':
       continue
    print(i)
else:
    print('666')

打印结果

猜你喜欢

转载自my.oschina.net/u/3648651/blog/1802358