-4

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.']

?

Aantar
  • 27
  • 7
  • 3
    What is the criteria? 6 characters per split? – A.J. Uppal May 29 '20 at 22:46
  • 2
    What have you tried so far? – G. Anderson May 29 '20 at 22:47
  • 2
    Please define the rule, according to which you want to perform the split. An example is not enough. Do you want to split into chunks of 6 characters? – Amitai Irron May 29 '20 at 22:47
  • 2
    Please read stackoverflow asking guidelines. This isn't a get your homework done site. Show us what you have come up with so far and where you are facing the problem. – Abhishek Bhagate May 29 '20 at 22:48
  • lol it's not my homework...I was working on my project which requires a lit python but I have only learned Java and C so I just completely not sure what method I should apply – Aantar May 29 '20 at 23:03

1 Answers1

3

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].

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76