我正在尝试编写一个程序来解决单词搜索难题,并向用户显示单词的位置,我想将字符串中所有匹配的单词大写。
例如, 'randomtextfoorandomtext'
变成 'randomtextFOOrandomtext'
我考虑过使用列表推导式,但我不确定如何以这种方式使用它。
请您参考如下方法:
实现此目的的最简单方法是使用 str.replace()方法:
puzzle = 'randomtextfoorandomtext'
word = 'foo'
highlighted = puzzle.replace(word, word.upper())
