【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')
如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取