Splitting a string into substrings of equal size

Emile van Sebille emile at fenx.com
Sat Aug 15 19:28:00 CEST 2009

On 8/14/2009 5:22 PM candide said...
> Suppose you need to split a string into substrings of a given size (except
> possibly the last substring). I make the hypothesis the first slice is at the
> end of the string.
> A typical example is provided by formatting a decimal string with thousands
> separator.
> What is the pythonic way to do this ?

I like list comps...

 >>> jj = '1234567890123456789'
 >>> ",".join([jj[ii:ii+3] for ii in range(0,len(jj),3)])


More information about the Python-list mailing list