[Tutor] Two More Questions

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 28 Feb 2001 21:35:40 -0800 (PST)


On Wed, 28 Feb 2001 alan.gauld@bt.com wrote:

> > >>> string.center('''hello world.
> > ... this is a test.''', 45)
> > '         hello world.\012this is a test.        '
> > ###
> > 
> > Hmmm... but it depends on the function if it handles a 
> > multi-line string properly or not.  string.center() 
> > certainly didn't handle that nicely.  

> Now whether thats what you expected centre to do so is another 
> matter, it certainly won't centre each line, but it doesn't 
> claim to do so.


Very true!  But I still would like it to do something different.  *grin*

Let me see if I can "fix" it:

###
>>> def mycenter(multiline, colwidth):
...     lines = string.split(multiline, '\n')
...     newlines = [string.center(l, colwidth) for l in lines]
...     return string.join(newlines, '\n')
...
>>> print mycenter("""Hello world!
... This is a test
... of centering a      
... multiline.""", 45)
                 Hello world!                
                This is a test               
                of centering a               
                  multiline.
###

Ah, this is better.

Subjectively speaking, of course.  *grin*