我正在尝试编写一个程序来解决单词搜索难题,并向用户展示单词在哪里,我想将字符串中所有匹配的单词大写。
例如,
'randomtextfoorandomtext'
变成
'randomtextFOOrandomtext'
我曾考虑过使用列表理解功能,但不确定如何以这种方式使用它。
实现此目的的最简单方法是使用str.replace()方法:
puzzle = 'randomtextfoorandomtext'
word = 'foo'
highlighted = puzzle.replace(word, word.upper())
Run Code Online (Sandbox Code Playgroud)