A 'Box' Function
mikelaskey at gmail.com
mikelaskey at gmail.com
Mon Apr 17 20:51:37 EDT 2006
Hey guys.
I should warn you, first off, that I'm relatively new to Python.
Basically, what I'm trying to do is create a word-wrapping function
with the added complication that it add a character at the beginning
and end of each line, so that it encloses the text in a sort of 'box':
----------------
| Like this |
----------------
The word-wrapping function I'm working with is similar to the one given
here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
def wrap(text, width):
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
' \n'[(len(line)-line.rfind('\n')-1
+ len(word.split('\n',1)[0]
) >= width)],
word),
text.split(' ')
)
Does anyone have any ideas on how it could be modified? Am I
approaching it the right way? Thanks a bunch!
Mike
More information about the Python-list
mailing list