Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
How to split string
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Chris, post: 3168139"] To be honest I couldn't think of a neat way of doing it. What seems to look right, albeit fairly ugleh was this. import string def digest_chunks(input_string, max_length): tmp = [] while input_string: if len(input_string) >= max_length: if input_string[max_length] not in string.whitespace: y = max_length - input_string[max_length::-1].find(' ') if not y: if len(input_string) < max_length: tmp.append(input_string) input_string = '' else: y = input_string[::-1].find(' ', 1) tmp.append(input_string[:y]) input_string = input_string[y:] else: tmp.append(input_string[:y]) input_string = input_string[y:] else: tmp.append(input_string[:max_length]) input_string = input_string[max_length:] else: tmp.append(input_string) input_string = '' return '\n'.join(tmp) s = 'this is just a random sequence of letters courtesy of monkeys on typewriters.' print digest_chunks(s, 20) this is just a random sequence of letters courtesy of monkeys on typewriters. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
How to split string
Top