[Tutor] word wrapping a string

Magnus Lycka magnus@thinkware.se
Tue Nov 12 02:39:03 2002


At 18:05 2002-11-11 -0800, Lance wrote:
>I would like to write text that word wraps to two or three levels of tab
>stops.

In what context?

A text file?
On screen display for an interactive program?
GUI?

The sharpest tool in the shed isn't always a python function...
Depending on the context, you might want some other program
that you want to use, rather than to reinvent this in Python.
I've never seen exactly this coded in Python, but it's no big
thing...

One option would be to create an HTML table and show that in
a browser (GUI or text mode). But it depends on what context
we're in, and what you are aiming for next. Will there me more
text formatting after this?

With some work it's not difficult to write a class that works
like this:

table = TextTable(8, 71)
table.addColumn('start')
table.addColumn('Hello World' * 30)
print table

You have to word-wrap each column according to it's length.
(Put the text in a list of strings.) Then, as you print, you
iterate over your rows and print column-line after column line.

It will probably be twenty lines of code or so though...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se