Hi all.
I Need a Solution split String a String into 40 char chunks.
The String is from a User Input and can be longer (between 1 and 290 Chars), but i Need to Split the String into max 40 char packets which will be padded by spaces to have a readable resulting text.
My First Idea was to Split the whole string by spaces as delimiter and rebuild the new 40 char Packs by Looping over the Split result. But question is, isnt there a smarter way to solve that ?
Can you provide an example of a string, and what you want as a result? I don't understand why you would split the long string into "words" before splitting into 40 character packs,
(Jul-31-2023, 06:58 PM)lastyle Wrote: [ -> ]max 40 char packets which will be padded by spaces to have a readable resulting text.
It seems that you want the built-in
textwrap module.
>>> import textwrap
>>> s = "The String is from a User Input and can be longer (between 1 and 290 Chars), but i Need to Split the String into max 40 char packets which will be padded by spaces to have a readable resulting text."
>>> print(textwrap.fill(s, width=40))
The String is from a User Input and can
be longer (between 1 and 290 Chars), but
i Need to Split the String into max 40
char packets which will be padded by
spaces to have a readable resulting
text.
>>>
Sure, lets say the String is
"Someone entered a fancy String Here, whooo it is way to Long. Therefor it has to be splitted"
Result should be a String Array Like :
"Someone entered a fancy String Here, "
"whooo it is way to Long. Therefor it has "
"to be splitted. "
Each array part padded to 40 chars
(Jul-31-2023, 07:19 PM)Gribouillis Wrote: [ -> ] (Jul-31-2023, 06:58 PM)lastyle Wrote: [ -> ]max 40 char packets which will be padded by spaces to have a readable resulting text.
It seems that you want the built-in textwrap module.
>>> import textwrap
>>> s = "The String is from a User Input and can be longer (between 1 and 290 Chars), but i Need to Split the String into max 40 char packets which will be padded by spaces to have a readable resulting text."
>>> print(textwrap.fill(s, width=40))
The String is from a User Input and can
be longer (between 1 and 290 Chars), but
i Need to Split the String into max 40
char packets which will be padded by
spaces to have a readable resulting
text.
>>>
Thanks, that looks what i was searching for.
Modules are great, but I like to know what's going on.
This is a simple task, you could do like this:
mystring = "Someone entered a fancy String Here, whooo it is way to Long. Therefore it has to be splitted, even it there is no word splitted"
def splitString(astring, chunksize):
for c in range(0, len(astring), chunksize):
splitpos = c + chunksize
if splitpos > len(astring):
splitpos = len(astring)
chunk = astring[c:splitpos]
print(chunk)
(Aug-01-2023, 08:01 AM)Pedroski55 Wrote: [ -> ]Modules are great, but I like to know what's going on.
There is a little more going on in the textwrap module because the
algorithm uses regexes to find nice places where to split the text. The chunk size is not the only criterion.
Ah well, I could make my function a little more "funcy"!