Is there a way to split a string by like splitting
"The quick brown fox jumps over the lazy dog."
to
['The qu', 'ick br', 'own fo', 'x jump', 's over', ' the l', 'azy do', 'g.']
?
Is there a way to split a string by like splitting
"The quick brown fox jumps over the lazy dog."
to
['The qu', 'ick br', 'own fo', 'x jump', 's over', ' the l', 'azy do', 'g.']
?
If you are trying to split by a particular number of characters, consider using a list comprehension alongside the step
parameter of range
:
>>> x = "The quick brown fox jumps over the lazy dog."
>>> N = 6
>>> [x[i:i+N] for i in range(0, len(x), N)]
['The qu', 'ick br', 'own fo', 'x jump', 's over', ' the l', 'azy do', 'g.']
>>>
range(0, len(x), N)
will return increments of N
until len(x)
. We may use this as the starting index for each slice, and then take N
characters after that index, as x[i:i+N]
.