
[英]How do I capitalize all occurrences of the word 'I' in a string? Python
[英]Python - How do I highlight a word in a string?
我正在尝试制作一个程序来解决单词搜索难题,并向用户显示单词的位置,我想将字符串中的所有匹配单词大写。
例如, 'randomtextfoorandomtext'
变成'randomtextFOOrandomtext'
我曾考虑过使用列表理解,但我不确定如何以这种方式使用它。
实现这一点的最简单方法是使用str.replace()方法:
puzzle = 'randomtextfoorandomtext'
word = 'foo'
highlighted = puzzle.replace(word, word.upper())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.