what is the pythonic efficient way to insert a character every n-characters in a string? for example
ins("aabbccdd", 2, "-") => "aa-bb-cc-dd"
is there a way to do this with iterators?
what is the pythonic efficient way to insert a character every n-characters in a string? for example
ins("aabbccdd", 2, "-") => "aa-bb-cc-dd"
is there a way to do this with iterators?
You can str.join
i
length chunks of s
:
s = "aabbccdd"
i = 2
print("-".join([s[j:j+i] for j in range(0,len(s),i)]))
aa-bb-cc-dd