当前位置:  首页>> 技术小册>> Python合辑2-字符串常用方法

30、swapcase()

翻转字符串中的字母大小写。

  1. s = 'HELLO world'
  2. s = s.swapcase()
  3. print(s)
  4. # hello WORLD

31、zfill()

  1. string.zfill(width)

返回长度为width的字符串,原字符串string右对齐,前面填充0。

  1. s = '42'.zfill(5)
  2. print(s)
  3. # 00042
  4. s = '-42'.zfill(5)
  5. print(s)
  6. # -0042
  7. s = '+42'.zfill(5)
  8. print(s)
  9. # +0042