如何使用python在字符串中的一定数量的字符后插入空格?

use*_*697 9 python string

我需要在字符串中的一定数量的字符后插入一个空格.文本是一个没有空格的句子,需要在每n个字符后用空格分隔.

所以它应该是这样的.

thisisarandomsentence
Run Code Online (Sandbox Code Playgroud)

我希望它返回:

this isar ando msen tenc e
Run Code Online (Sandbox Code Playgroud)

我的功能是:

def encrypt(string, length):
Run Code Online (Sandbox Code Playgroud)

无论如何在python上做到这一点?

msh*_*yem 17

def encrypt(string, length):
    return ' '.join(string[i:i+length] for i in range(0,len(string),length))
Run Code Online (Sandbox Code Playgroud)

encrypt('thisisarandomsentence',4)

'this isar ando msen tenc e'
Run Code Online (Sandbox Code Playgroud)