How do I change a string so that every 5 characters there will be a space in between? However, I do not wish to have space at the beginning or at the end.
for example
"Yohomeyoverstack" will be "Yohom eyove rstac k"
"two" will be "two"
How do I change a string so that every 5 characters there will be a space in between? However, I do not wish to have space at the beginning or at the end.
for example
"Yohomeyoverstack" will be "Yohom eyove rstac k"
"two" will be "two"
Using slicing
Ex:
s = "Yohomeyoverstack"
print( " ".join([s[i:i+5] for i in range(0, len(s), 5)]) )
Output:
Yohom eyove rstac k
You can do this with the help of slicing.
data = "Yohomeyoverstack"
stripped = "".join(data.split())
splitted = " ".join([stripped[i:i+5] for i in range(0, len(stripped), 5)])
print(splitted)
This solution will work even if there are whitespaces in the string