Skip to content
Advertisement

Split string into strings by length?

Is there a way to take a string that is 4*x characters long, and cut it into 4 strings, each x characters long, without knowing the length of the string?

For example:

>>>x = "qwertyui"
>>>split(x, one, two, three, four)
>>>two
'er'

Advertisement

Answer

>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)//4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement