python,字符串方法

Leo*_*Leo 0 python string split

可能重复:
Pythonic方式在字符串中插入每2个元素

如果有人可以帮助python代码我会很高兴))我怎样才能将空格放入一个字符串中,例如,

如果有字符串'akhfkahgdsds' 我想把它变成'ak hf ka hg ds ds'

Pao*_*tti 7

>>> s = 'akhfkahgdsds'
>>> range(0, len(s), 2) # gives you the start indexes of your substrings
[0, 2, 4, 6, 8, 10]
>>> [s[i:i+2] for i in range(0, len(s), 2)] # gives you the substrings
['ak', 'hf', 'ka', 'hg', 'ds', 'ds']
>>> ' '.join(s[i:i+2] for i in range(0, len(s), 2)) # join the substrings with spaces between them
'ak hf ka hg ds ds'
Run Code Online (Sandbox Code Playgroud)