how do i insert spaces into a string using the range function?

后端 未结 3 1484
再見小時候
再見小時候 2021-02-15 15:37

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

3条回答
  •  暖寄归人
    2021-02-15 16:24

    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.

提交回复
热议问题