Python Forum
How to split a String from Text Input into 40 char chunks? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to split a String from Text Input into 40 char chunks? (/thread-40460.html)



How to split a String from Text Input into 40 char chunks? - lastyle - Jul-31-2023

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 ?


RE: How to split a String from Text Input into 40 char chunks? - deanhystad - Jul-31-2023

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,


RE: How to split a String from Text Input into 40 char chunks? - Gribouillis - Jul-31-2023

(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.
>>> 



RE: How to split a String from Text Input into 40 char chunks? - lastyle - Jul-31-2023

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


RE: How to split a String from Text Input into 40 char chunks? - lastyle - Jul-31-2023

(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.


RE: How to split a String from Text Input into 40 char chunks? - Pedroski55 - Aug-01-2023

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)



RE: How to split a String from Text Input into 40 char chunks? - Gribouillis - Aug-01-2023

(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.


RE: How to split a String from Text Input into 40 char chunks? - Pedroski55 - Aug-01-2023

Ah well, I could make my function a little more "funcy"!