Basic Python tutorial 2-3: print a sentence in the "box" with the correct width. Basic python tutorial
Sample Code:
# Obtain the sentence length
Sentence = input ('plese input a sentence: ') # He's very naughty boy
Screen_width = 100
# Getting the length of Text
Text_width = len (sentence)
# Text width
Box_width = text_width + 10
# Calculate the number of free formats on the left and right sides [left edge, right edge]
Left_margin = (screen_width-box_width) // 2
Box_left_margin = (box_width-text_width) // 2
# Print screen width
Print ('=' x 100)
Print (''' * left_margin + '-' * (box_width-2) + ')
Print (''' * left_margin + '|' + ''' * (box_width-2) + '| ')
Print (''' * left_margin + '|' + ''' * (box_left_margin-1) + sentence + ''' * (box_left_margin-1) + '| ')
Print (''' * left_margin + '|' + ''' * (box_width-2) + '| ')
Print (''' * left_margin + '-' * (box_width-2) + ')
# Print screen width
Print ('=' x 100)
Running result:
Plese input a sentence: I love you Fiona
========================================================== ========================================================== ================================
+ ------------------------ +
|
| I love you Fiona |
|
+ ------------------------ +
========================================================== ========================================================== ================================