【Python】字符串不⾜位数补零补空格⼀、补零
if__name__ == '__main__':
str1 = '130000'
for a in range(100):
字符串长度不够后面补0公式print(str1 + str(a).zfill(5))
⼆、补空格
2.1、rjust:右对齐
if__name__ == '__main__':
str_1 = ['cscs', 'dad', 'a', 'adasdadadad', 'jdsad', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
for s in str_1:
print(s.rjust(30), ':', '11')
2.2、ljust:左对齐
if__name__ == '__main__':
str_1 = ['cscs', 'dad', 'a', 'adasdadadad', 'jdsad', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
for s in str_1:
print(s.ljust(30), ':', '11')
2.3、center:居中对齐
if__name__ == '__main__':
str_1 = ['cscs', 'dad', 'a', 'adasdadadad', 'jdsad', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
for s in str_1:
(30), ':', '11')

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。