当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

计算Python中字符串中大写和小写字符的数量

  1. string = "asdfHRbySFss"
  2. uppers = [l for l in string if l.isupper()]
  3. print(len(uppers))
  4. lowers = [l for l in string if l.islower()]
  5. print(len(lowers))

Output:

  1. 4
  2. 8