这是我的解决方案(希望我能很好地理解你的问题):
-
你把句子分成了几个词。
-
你把所有的单词循环一遍
-
你从每个单词的第二个字母开始循环单词的字母。
-
检查是否有大写字母
-
如果你找到大写字母,你给have_to_replace变量加1。
-
你来替换
txt = 'My team is the BeST team at the tournament'
x = txt.split()
for w in x:
have_to_replace = False
for l in w[1:]:
if l != l.casefold():
have_to_replace = True
if have_to_replace:
txt = txt.replace(w, 'XXX_', 1)
print(txt)
由于某些原因,前三行(以及最后一行)的代码阻塞无法正常工作。抱歉。