If I have a string, for example which reads: 'Hello how are you today Joe' How am I able to insert spaces into it at regular intervals? So for example I want to insert spaces into it using the range function in these steps: range(0,27,2). So it will look like this:
"He ll o ho w ar e yo u to da y Jo e"
It now has a space at every 2nd index going up to it's end. How do I do this does anyone know? thanks.
The most straight-forward approach for this particular case is
s = 'Hello how are you today Joe'
s = " ".join(s[i:i+2] for i in range(0, len(s), 2))
This splits the string into chunks of two characters each first, and then joins these chunks with spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With