Iterate over slices of a string

def iter_slices(string, slice_length):
"""Iterate over slices of a string."""
pos = 0
if slice_length is None or slice_length <= 0:
slice_length = len(string)
while pos < len(string):
yield string[pos:pos + slice_length]
pos += slice_length

s = "thisisatest"
o = 2
p = iter_slices(s,o)
print p
print (p.next())
print (p.next())
print (p.next())
print (p.next())

猜你喜欢

转载自www.cnblogs.com/tnyleyon/p/9452844.html
今日推荐