python打印右对齐_python右对齐的实例方法
例如,有一个字典如下:
>>> dic = {
"name": "botoo",
"url": "//www.94e.cn",
"page": "88",
"isNonProfit": "true",
"address": "china",
}
想要得到的输出结果如下:
name:botoo
url:https:www.94e.cn
page:88
isNonProfit:ture
address:china
首先获取字典的最大值max(map(len, dic.keys()))
然后使用
Str.rjust() 右对齐
或者
Str.ljust() 左对齐
或者
Str.center() 居中的方法有序列的输出。
>>> dic = {
"name": "botoo",
"url": "//www.jb51.net",
"page": "88",
"isNonProfit": "true",
"address": "china",
}
>>>
>>> d = max(map(len, dic.keys())) #获取key的最大值
>>>
>>> for k in dic:
print(k.ljust(d),":",dic[k])
name : botoo
url : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>> for k in dic:
print(k.rjust(d),":",dic[k])
name : botoo
url : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>> for k in dic:
print(k.center(d),":",dic[k])
name : botoo
url : //www.jb51.net
page : 88
isNonProfit : true
address : china
>>>
关于 str.ljust()的用法还有这样的;
>>> s = "adc"
>>> s.ljust(20,"+")
'adc+++++++++++++++++'
>>> s.rjust(20)
'adc'
>>> s.center(20,"+")
'++++++++adc+++++++++'
>>>
知识点扩展:
python中对字符串的对齐操作
ljust()、rjust() 和 center()函数分别表示左对齐、右对齐、居中对齐
str.ljust(width[, fillchar]):左对齐,width -- 指定字符串长度,fillchar -- 填充字符,默认为空格;
str.rjust(width[, fillchar]):右对齐,width -- 指定字符串长度,fillchar -- 填充字符,默认为空格;
str.center(width[, fillchar]):居中对齐,width -- 字符串的总宽度,fillchar -- 填充字符,默认为空格。
test = 'hello world'
print(test.ljust(20))
print(test.ljust(20, '*'))
print(test.rjust(20, '*'))
print(test.center(20, '*'))
print(test.center(20))
#输出结果如下:
hello world*********
*********hello world
****hello world*****
hello world
到此这篇关于python右对齐的实例方法的文章就介绍到这了,更多相关python中如何右对齐内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!
python打印右对齐_python右对齐的实例方法相关推荐
- python第一行左对齐_python 左对齐,右对齐
python 左对齐,右对齐 >>> print('{} and {}'.format('hello','world')) # 默认左对齐 hello and world >& ...
- Python 打印的中英文字体如何对齐?
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 打印中英文字体如何对齐? 在Python下对齐打印中英文混合字符串 1.初步想法:利用字符串函数Format()格式化输出 ...
- python字符串对齐_Python - 字符串对齐
字符串对齐 本文地址: http://blog.csdn.net/caroline_wendy/article/details/20463231 Python中, 字符串对齐, 使用ljust(), ...
- python打印换行符_Python换行符以及如何在不使用换行符的情况下进行Python打印
python打印换行符 Welcome! The new line character in Python is used to mark the end of a line and the begi ...
- python 打印列表元素_Python程序以不同方式打印列表元素
python 打印列表元素 In this program – we are going to learn how can we print all list elements, print spec ...
- python打印到文件_Python打印到文件
python打印到文件 1. Python打印到文件 (1. Python Print to File) In this lesson, we will see how we can route ou ...
- python打印金字塔三角形_python打印星号金字塔的方法
python打印星号金字塔的方法 用星号输出一个金字塔(行数自定) 推荐:<python教程> 解题思路: ①定义一个函数来进行封装,名字任取 ②计算每行需要输出的空格的数量 (行数减一) ...
- python打印空心长方形_Python程序打印空心半菱形星形图案
给出一个整数N,任务是打印空心半菱形图案.示例:输出:## ## ## ## ## ##输入:7## ## ## ## ## ## ## #### ## ## ## #下半部分:对于下半部分,使用迭代 ...
- python左右同时对齐_python怎么对齐
在python中经常会使用到打印,但是在打印表格时经常遇到中英文对齐问题,怎么解决呢? 往往要对输出内容进行对齐,看起来更清爽.(推荐学习:Python视频教程) 通过ljust(),center() ...
- python中文对齐_Python中英文对齐终极解决方案
原博文 2020-05-14 17:43 − ## 问题描述 强迫症发作 ## 对齐结果 ![](https://img2020.cnblogs.com/blog/1418366/202005/141 ...
最新文章
- DCD DSR DTR RTS CTS表示什么意思
- boost::mpl::less_equal相关的测试程序
- 英特尔软件学院与中国一起迎接2008
- art-template入门(三)之语法
- SCU 4439 Vertex Cover(二分图最小覆盖点)题解
- java 延迟实例化_延迟初始化Spring Bean的几种方式
- aws lambda_Express.js和AWS Lambda —无服务器的爱情故事
- 人工智能撰稿将代替人工写作?我们拭目以待
- pandas入门(一):pandas的安装和创建
- jpg/png格式的图片转换成eps格式
- QQ名片点赞全自动引流脚本,基于autojs的
- 计算机xp画板在哪找,xp系统画图工具在哪里|xp画图工具有什么功能
- chrome翻译插件之谷歌翻译
- 计算机有网络但打不开网页,手把手教你有网络但是打不开网页怎么办?
- 微信公众号第三方平台授权流程
- 稳定性测试-几点总结
- iOS 10 跳转系统设置
- git merge request
- 尚硅谷前端项目开发笔记
- 区块链隐私保护:MimbleWimble 和 Grin 简介