Python基础教程2-3:以正确的宽度在居中的“盒子”内打印一个句子
2024-08-30 03:36:20
代码示例:
#获取句子长度
sentence = input('Plese input a sentence:')#He's very naughty boy
screen_width =100
#获取文本的长度
text_width =len(sentence)
#文本的宽度
box_width = text_width +10
#计算出左右两边需空余的格式数[左边缘,右边缘]
left_margin = (screen_width - box_width)//2
box_left_margin = (box_width-text_width)//2 #打印屏幕宽度
print('='*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('='*100)
运行结果:
Plese input a sentence:I love you Fiona
====================================================================================================
+------------------------+
| |
| I love you Fiona |
| |
+------------------------+
====================================================================================================
最新文章
- golang print struct with key
- Maven私服Nexus3.x环境构建操作记录
- yii2权限控制rbac之rule详细讲解(转)
- LTE工作过程
- 蓝牙的OBEX协议
- crontab 定时任务格式
- window.print打印指定div实例代码
- 5A的肖特基二极管 SK5x / SK5xx
- linux命令ps aux|grep xxx详解
- 简易的highcharts公共绘图模块封装--基于.net mvc
- windows 配置host
- iOS 将UIColor转换为UIImage
- FZU 2256 迷宫
- NopCommerce开源项目中很基础但是很实用的C# Helper方法
- MVC 使用缓存
- Codeforces Round #436 (Div. 2)D. Make a Permutation! 模拟
- forEach 如何提前终止 跳出运行
- HTML&;javaSkcript&;CSS&;jQuery&;ajax(三)
- NLP 工具类库
- bigtable原理
热门文章
- java.sql.SQLException: Could not commit with auto-commit set on
- 关于Mac系统中my sql 和navicat for mysql 的连接问题。
- Linux基本系统优化
- Codeforces 1167E(思路、数据处理)
- ASP .NET Core 2.1 HTTP Error 502.5 – Process Failure
- Ubuntu14.04 使用scp远程传输命令进行服务器文件互传
- AngularJS(一):概述
- mysql操作封装
- 利用bintray-release插件上传到Bintray- HTTP/1.1 404 Not Found [message:Repo 'maven' was not found]问题解决
- 【js类库Raphaël】使用raphael.js根据点坐标绘制平滑曲线