小册名称:Python合辑4-130个字符串操作示例
字符串格式化填充负数
n = [-2, -8, 1, -10, 40]num = ["{1:0{0}d}".format(2 if x >= 0 else 3, x) for x in n]print(num)
n = [-2, -8, 1, -10, 40]
num = ["{1:0{0}d}".format(2 if x >= 0 else 3, x) for x in n]
print(num)
Output: