=0else3,x)forxinn]print(num)```Output:``"/>
当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

字符串格式化填充负数

  1. n = [-2, -8, 1, -10, 40]
  2. num = ["{1:0{0}d}".format(2 if x >= 0 else 3, x) for x in n]
  3. print(num)

Output:

  1. n = [-2, -8, 1, -10, 40]
  2. num = ["{1:0{0}d}".format(2 if x >= 0 else 3, x) for x in n]
  3. print(num)

该分类下的相关小册推荐: