Python center string using format specifier

formatting, python, python-3.2, python-3.x, string-formatting

Solution

You need to centre each line separately:

'\n'.join('{:^80}'.format(s) for s in message.split('\n'))

Problem

I have a string called `message`. ``` message = "Hello, welcome!\nThis is some text that should be centered!" ``` And I'm trying to center it for a default Terminal window, i.e. of 80 width, with this statement: ``` print('{:^80}'.format(message)) ``` Which prints: ``` Hello, welcome! This is some text that should be centered! ``` I'm expecting something like: ``` Hello, welcome! This is some text that should be centered! ``` Any suggestions?

Original source