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

根据 Python 中的索引位置将字符串大写

  1. def capitalize(s, ind):
  2. split_s = list(s)
  3. for i in ind:
  4. try:
  5. split_s[i] = split_s[i].upper()
  6. except IndexError:
  7. print('Index out of range : ', i)
  8. return "".join(split_s)
  9. print(capitalize("abracadabra", [2, 6, 9, 10, 50]))

Output:

  1. Index out of range : 50
  2. abRacaDabRA