Python Tutorial

Thursday, July 21, 2011

String split by length

Python String split by length:
some time we need to split string by length, sample code is given below

def split_by_length(s,block_size):
    w=[]
    n=len(s)
    for i in range(0,n,block_size):
        w.append(s[i:i+block_size])
    return w
w=split_by_length("ABCDEFGHIJKLMNOPQRSTUVWXYZ",5)
print w




Output:

['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST', 'UVWXY', 'Z']

1 comments:

wind west said...
This comment has been removed by the author.

Post a Comment