gpt4 book ai didi

Python - 如何突出显示字符串中的单词?

转载 作者:太空宇宙 更新时间:2023-11-04 07:49:04 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来解决单词搜索难题,并向用户显示单词的位置,我想将字符串中所有匹配的单词大写。

例如,'randomtextfoorandomtext'

变成'randomtextFOOrandomtext'

我考虑过使用列表推导式,但我不确定如何以这种方式使用它。

最佳答案

实现此目的的最简单方法是使用 str.replace()方法:

puzzle = 'randomtextfoorandomtext'
word = 'foo'
highlighted = puzzle.replace(word, word.upper())

关于Python - 如何突出显示字符串中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58076516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com