Problem 2.3

Text in a Box

Write a program box.py that takes word as a command-line argument and prints the word in a box as shown below.

$ python box.py python
+--------+
| python |
+--------+

Please note that there should be exactly one space on either side of the text in the box.

Solution

import sys

def box(word):
    n = len(word) + 2
    line = "-" * n
    header = f"+{line}+"
    print(header)
    print(f"| {word} |")
    print(header)

word = sys.argv[1]
box(word)