首页 > 编程语言 > 详细

python 字符串补全填充固定长度(补0)的三种方法

时间:2021-06-12 17:44:58      阅读:46      评论:0      收藏:0      [点我收藏+]

python 字符串补全填充固定长度(补0)的三种方法

text justification
‘‘‘
原字符串左侧对齐, 右侧补零:
‘‘‘
str.ljust(width,‘0‘)
input: ‘789‘.ljust(32,‘0‘)
output: ‘78900000000000000000000000000000‘


‘‘‘
原字符串右侧对齐, 左侧补零:
方法一:
‘‘‘
str.rjust(width,‘0‘)
input: ‘798‘.rjust(32,‘0‘)
output: ‘00000000000000000000000000000798‘
‘‘‘
方法二:
‘‘‘
str.zfill(width)
input: ‘123‘.zfill(32)
output:‘00000000000000000000000000000123‘
‘‘‘
方法三:
‘‘‘
‘%07d‘ % n
input: ‘%032d‘ % 89
output:‘00000000000000000000000000000089‘

python 字符串补全填充固定长度(补0)的三种方法

原文:https://www.cnblogs.com/xushenghai19841013/p/14878469.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!