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

如何在 Python 中将字符串的第三个字母大写

  1. s = "xxxyyyzzz"
  2. # convert to list
  3. a = list(s)
  4. # change every third letter in place with a list comprehension
  5. a[2::3] = [x.upper() for x in a[2::3]]
  6. # back to a string
  7. s = ''.join(a)
  8. print(s)

Output:

  1. xxXyyYzzZ

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