Splitting a string into substrings of equal size
Gregor Lingl
gregor.lingl at aon.at
Sat Aug 15 14:50:03 EDT 2009
Emile van Sebille schrieb:
> On 8/14/2009 5:22 PM candide said...
...
>> 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)])
> '123,456,789,012,345,678,9'
> >>>
>
> Emile
>
Less beautiful but more correct:
>>> ",".join([jj[max(ii-3,0):ii] for ii in
range(len(jj)%3,len(jj)+3,3)])
'1,234,567,890,123,456,789'
Gregor
More information about the Python-list
mailing list