Im trying to create somthing which splits a string into a block of 4 characters. So lets say it obtains the string: "HELLOWORLDIAMAROBOT"
it would then format it to:
HELL
OWOR
LDIA
MARO
BOT
Thanks
Im trying to create somthing which splits a string into a block of 4 characters. So lets say it obtains the string: "HELLOWORLDIAMAROBOT"
it would then format it to:
HELL
OWOR
LDIA
MARO
BOT
Thanks
This is one way to do it.
string = "HELLOWORLDIAMAROBOT"
def split_string(string, split_string):
return [string[i:i+split_string] for i in range(0, len(string), split_string)]
print (split_string(string,4))
Output:
['HELL', 'OWOR', 'LDIA', 'MARO', 'BOT']
This can be done in a one-liner:
[string[i:i+4] for i in range(0, len(string), 4)]