Basic Python tutorial 2-3: print a sentence in the "box" with the correct width. Basic python tutorial

Source: Internet
Author: User

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 |
|
+ ------------------------ +
========================================================== ========================================================== ================================

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.