Python Forum
How to split a String from Text Input into 40 char chunks?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split a String from Text Input into 40 char chunks?
#1
Sad 
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 ?
Reply
#2
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,
Reply
#3
(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.
>>> 
lastyle likes this post
Reply
#4
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
Reply
#5
(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.
Reply
#6
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)
Reply
#7
(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.
Reply
#8
Ah well, I could make my function a little more "funcy"!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using string input for boolean tronic72 3 710 Nov-01-2023, 07:48 AM
Last Post: Gribouillis
  doing string split with 2 or more split characters Skaperen 22 2,563 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  [split] Parse Nested JSON String in Python mmm07 4 1,549 Mar-28-2023, 06:07 PM
Last Post: snippsat
Question Take user input and split files using 7z in python askfriends 2 1,110 Dec-11-2022, 07:39 PM
Last Post: snippsat
  How to split the input taken from user into a single character? mHosseinDS86 3 1,187 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  is there an itertor of chunks? Skaperen 8 1,966 Jul-22-2022, 10:17 PM
Last Post: Skaperen
  Split string using variable found in a list japo85 2 1,311 Jul-11-2022, 08:52 AM
Last Post: japo85
  Editing text between two string from different lines Paqqno 1 1,324 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
Big Grin General programming question (input string)[ jamie_01 2 1,611 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Split string knob 2 1,883 Nov-19-2021, 10:27 AM
Last Post: ghoul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020