以下代码功能是:不论你输入一句多长的句子,都能建造相应大小盒子并且语句在盒子居中。。。
#!/usr/bin/env python
#定义
sentence = raw_input("Sentence: ")
screen_with = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_with - text_width) // 2
#画盒子
print
print ‘ ‘ * left_margin + ‘+‘ + ‘-‘ * box_width + ‘+‘
print ‘ ‘ * left_margin + ‘|‘ + ‘ ‘ * box_width + ‘|‘
print ‘ ‘ * left_margin + ‘|‘ + ‘ ‘ * ((box_width - text_width) // 2)  + sentence  + ‘ ‘ * ((box_width - text_width) // 2) + ‘|‘
print ‘ ‘ * left_margin + ‘|‘ + ‘ ‘ * box_width + ‘|‘
print ‘ ‘ * left_margin + ‘+‘ + ‘-‘ * box_width + ‘+‘
print本文出自 “kysida” 博客,请务必保留此出处http://hongtengfei.blog.51cto.com/9881650/1706064
原文:http://hongtengfei.blog.51cto.com/9881650/1706064