python实现指定字符串补全空格、前面填充0的方法
作者:流风,飘然的风 时间:2022-04-06 21:13:58
Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0。
zfill()方法语法:str.zfill(width)
参数width -- 指定字符串的长度。原字符串右对齐,前面填充0。
返回指定长度的字符串。
以下实例展示了 zfill()函数的使用方法:
#!/usr/bin/python
str = "this is string example....wow!!!";
print str.zfill(40);
print str.zfill(50);
以上实例输出结果如下:
00000000this is string example....wow!!!
000000000000000000this is string example....wow!!!
zfill()则用于向数值的字符串表达式左侧填充0, 该函数可以正确理解正负号:
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'
=====================================
在Python中打印字符串时可以调用ljust(左对齐),rjust(右对齐),center(中间对齐)来输出整齐美观的字符串
python实现指定字符串补全空格的方法:
如果希望字符串的长度固定,给定的字符串又不够长度,我们可以通过rjust,ljust和center三个方法来给字符串补全空格
rjust,向右对其,在左边补空格
s = "123".rjust(5) assert s == " 123"
ljust,向左对其,在右边补空格
s = "123".ljust(5) assert s == "123 "
center,让字符串居中,在左右补空格
s = "123".center(5) assert s == " 123 "
总结
以上所述是小编给大家介绍的python实现指定字符串补全空格、前面填充0的方法 网站的支持!
来源:https://www.cnblogs.com/zdz8207/p/python_learn_note_21.html
标签:python,字符串,空格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 字节流,字符串,十六进制相互转换实例(binascii,bytes)
2022-04-24 02:59:36
![](https://img.aspxhome.com/file/2023/9/67419_0s.jpg)
ubuntu 16.04下python版本切换的方法
2021-07-10 16:36:04
![](https://img.aspxhome.com/file/2023/1/67871_0s.png)
Django model 中设置联合约束和联合索引的方法
2023-09-24 09:14:15
Javascript实现通过选择周数显示开始日和结束日的实现代码
2023-08-23 13:44:52
django请求返回不同的类型图片json,xml,html的实例
2021-05-17 10:03:23
Python中使用Frozenset对象的案例详解
2023-09-27 09:36:32
Python paramiko 模块浅谈与SSH主要功能模拟解析
2023-10-01 11:06:28
![](https://img.aspxhome.com/file/2023/7/66007_0s.png)
Web 设计:实现干净代码的12条定律
2008-12-04 13:27:00
![](https://img.aspxhome.com/file/UploadPic/200812/4/22008111521256-30.gif)
python交易记录整合交易类详解
2022-09-15 20:18:37
![](https://img.aspxhome.com/file/2023/9/63939_0s.png)
试试把xml和javascript写到同一个文件里面
2009-10-02 16:53:00
多语言网站开发 不完全技术分析
2008-02-18 12:52:00
Dreamweaver使用技巧--让css使网页图片半透明
2010-09-05 21:13:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723112956373s.jpg)
用SQL语句生成带有小计合计的数据集脚本
2009-01-06 11:33:00
Go如何实现HTTP请求限流示例
2023-07-21 00:40:27
php中加密解密DES类的简单使用方法示例
2023-09-07 23:28:44
![](https://img.aspxhome.com/file/2023/3/554131_0s.png)
Python 计算任意两向量之间的夹角方法
2022-11-10 07:01:47
![](https://img.aspxhome.com/file/2023/7/72787_0s.jpg)
轻松掌握怎样从Windows命令行启动MySQL
2009-02-23 17:18:00
Python实现监控键盘鼠标操作示例【基于pyHook与pythoncom模块】
2023-03-22 19:15:40
![](https://img.aspxhome.com/file/2023/0/78770_0s.png)
python 密码验证(滑块验证)
2021-01-24 02:32:18
![](https://img.aspxhome.com/file/2023/1/67361_0s.png)
Python疫情数据可视化分析
2022-05-22 05:09:15
![](https://img.aspxhome.com/file/2023/1/67791_0s.png)