Drawing box around message
<p>I'm working on this Python task that I can't figure out. It is the last of 3 functions and the first 2 were much easier to program then this one. The instructions are 
"Given a message that may contain multiple lines, utilize the split() function to identify the individual lines, and the format() function so that when printed, it draws a box around the message's lines, all centered. Box uses vertical bars &amp; dashes on the sides (|, -), +'s in the corners (+), and there is always a column of spaces to the left and right of the widest line of the message." </p>

<p>Some examples for what this function needs to do: </p>

<p>test that: border_msg('a') == '+---+\n| a |\n+---+\n'</p>

<p>test that: border_msg('hello') == '+-------+\n| hello |\n+-------+\n'</p>

<p>test that: border_msg("hi!\nhow are you?\ndrive safely!") == '+---------------+\n| hi! |\n| how are you? |\n| drive safely! |\n+---------------+\n'</p>

<p>I think it needs to print the above tests so that the words in the middle are surrounded by the "+------+ on the top and bottom and "|"'s on the sides.</p>

<p>Here is the code I have so far. I'm not sure where I would go from here.</p>

<pre><code>def border_msg(msg):
    border_msg.split("\n")
    '%s'.format(msg)
    return border_msg(msg)
    print border_msg(msg)
</code></pre>

<p>I've stitched up a little piece of code which implements the boxed messages. To be honest it's not the nicest piece of code, but it does the job and hopefully will help you to do it yourself (and better). For that purpose I've decided not to include comment, so you have to think that through for yourself. Maybe not the best educational method, but let's try it anyway:]</p>

<p>Code on <a href="https://github.com/quapka/expecto-anything/blob/master/boxed_msg.py" rel="nofollow"><code>Github</code></a>.</p>

<pre><code>from math import ceil, floor

def boxed_msg(msg):
    lines = msg.split('\n')
    max_length = max([len(line) for line in lines])
    horizontal = '+' + '-' * (max_length + 2) + '+\n'
    res = horizontal
    for l in lines:
        res += format_line(l, max_length)
    res += horizontal
    return res.strip()

def format_line(line, max_length):
    half_dif = (max_length - len(line)) / 2 # in Python 3.x float division
    return '| ' + ' ' * ceil(half_dif) + line + ' ' * floor(half_dif) + ' |\n'

print(boxed_msg('first_line\nsecond_line\nthe_ultimate_longest_line'))
# +---------------------------+
# |         first_line        |
# |        second_line        |
# | the_ultimate_longest_line |
# +---------------------------+
</code></pre>

python drawing message rectangles