OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

A good way to make long strings wrap to newline?

  • Thread starter Thread starter Joshua Merriman
  • Start date Start date
J

Joshua Merriman

Guest
In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly.

I want to be able to have Python read the string, then test if it is over 75 characters in length. If it is, then split the string up into multiple strings, then print one after the other on a new line. I also want it to be smart, not cutting off full words. i.e. "The quick brown <newline> fox..." instead of "the quick bro<newline>wn fox...".

I've tried modifying similar code that truncates the string after a set length, but just trashes the string instead of putting it in a new line.

What are some methods I could use to accomplish this?
<p>In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly.</p>

<p>I want to be able to have Python read the string, then test if it is over 75 characters in length. If it is, then split the string up into multiple strings, then print one after the other on a new line.
I also want it to be smart, not cutting off full words. i.e. <code>"The quick brown <newline> fox..."</code> instead of <code>"the quick bro<newline>wn fox..."</code>.</p>

<p>I've tried modifying similar code that truncates the string after a set length, but just trashes the string instead of putting it in a new line.</p>

<p>What are some methods I could use to accomplish this?</p>
 

Latest posts

Top