Splitting a string into groups of three characters

John Machin sjmachin at lexicon.net
Sun Aug 7 22:18:42 EDT 2005


lemon97 at gmail.com wrote:
> Hi,
> 
> Is there a function that split a string into groups, containing an "x"
> amount of characters?
> 
> Ex.
> TheFunction("Hello World",3)
> 
> Returns:
> 
> ['Hell','o W','orl','d']
> 
> 
> Any reply would be truly appreciated.
> 
> Thank You,
> 

Maybe, somewhere out there -- you could write one yourself, like this:

# untested
def nsplit(s, n):
    return [s[k:k+n] for k in xrange(0, len(s), n)]



More information about the Python-list mailing list