2}:{:0>2}".format(hour,minute))print("{:0>3}:{:0>5}".f"/>
当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

在 Python3 中用前导零填充字符串

  1. hour = 4
  2. minute = 3
  3. print("{:0>2}:{:0>2}".format(hour, minute))
  4. print("{:0>3}:{:0>5}".format(hour, minute))
  5. print("{:0<3}:{:0<5}".format(hour, minute))
  6. print("{:$<3}:{:#<5}".format(hour, minute))

Output:

  1. 04:03
  2. 004:00003
  3. 400:30000
  4. 4$$:3####