小册名称:Python合辑4-130个字符串操作示例
用零填充数字字符串
num = 123print('{:<08d}'.format(num))print('{:>08d}'.format(num))string = '123'print(string.ljust(8, '0'))print(string.rjust(8, '0'))print(string[::-1].zfill(8)[::-1])
num = 123
print('{:<08d}'.format(num))
print('{:>08d}'.format(num))
string = '123'
print(string.ljust(8, '0'))
print(string.rjust(8, '0'))
print(string[::-1].zfill(8)[::-1])
Output:
1230000000000123123000000000012312300000
12300000
00000123