Print list items in a rectangular frame The aim is to have that:

list ["Hello", "World", "in", "a", "frame"] 


And make it printed like this:




*********
* Hello *
* World *
* in    *
* a     *
* frame *
*********



I did this and it works fine:



a = 0
for nb in listA:
    if a == 0:
        print("*"*10).center(20)
    a += 1
    if len(nb) == 2:
        print("*   " + nb + "   *").center(20)
    elif len(nb) == 1:
        print("*   " + nb + "    *").center(20)
    else:
        print("* " + nb + "  *").center(20)
    if a == len(listA):
        listA[-1]
        print("*"*10).center(20)


Still, I feel it is very cumbersome. Do you have any ideas how I can improve it, please?
You can let .format() do most of the work for you:

def print_in_a_frame(*words):
    size = max(len(word) for word in words)
    print('*' * (size + 4))
    for word in words:
        print('* {:&lt;{}} *'.format(word, size))
    print('*' * (size + 4))

&gt;&gt;&gt; print_in_a_frame("Hello", "World", "in", "a", "frame")
*********
* Hello *
* World *
* in    *
* a     *
* frame *
*********


Most of the magic happens in this line:

print('* {:&lt;{}} *'.format(word, size))


It may be easier to understand what's going on if we rewrite it as:

print('* {a:&lt;{b}} *'.format(a=word, b=size))


This tells format to print the keyword argument a, left-aligned, on a field of total width the keyword argument b.  Note that if b was fixed, say to 10, you would write that as:

print('* {a:&lt;10}} *'.format(a=word))


or:

print('* {:&lt;10}} *'.format(word))