【Python】字符串不足位数补零/补空格

一、补零

if __name__ == '__main__':
    str1 = '130000'
    for a in range(100):
        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:
        print(s.center(30), ':', '11')

 

posted @ 2020-08-04 23:05  淡怀  阅读(12575)  评论(0编辑  收藏  举报