Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数,用于在传递sentense和word时将单词从sentense改为大写_Python_Function - Fatal编程技术网

Python函数,用于在传递sentense和word时将单词从sentense改为大写

Python函数,用于在传递sentense和word时将单词从sentense改为大写,python,function,Python,Function,我试图创建一个单行函数,将单词从句子改为大写。请建议 def highlight_word(sentence, word): return(___) 这应该符合您的要求,使用string.replace()和string.upper() 代码: def highlight_word(sentence, word): return sentence.replace(word, word.upper()) >>> highlight_word('I like P

我试图创建一个单行函数,将单词从句子改为大写。请建议

def highlight_word(sentence, word):
    return(___)

这应该符合您的要求,使用
string.replace()
string.upper()

代码:

def highlight_word(sentence, word):
    return sentence.replace(word, word.upper())
>>> highlight_word('I like Python', 'like')
'I LIKE Python'
用法:

def highlight_word(sentence, word):
    return sentence.replace(word, word.upper())
>>> highlight_word('I like Python', 'like')
'I LIKE Python'

欢迎来到StackOverflow。因此,它不是一个代码编写服务。请提供一些示例输入、所需输出以及您遇到的任何问题。只需注意替换词作为另一个词的一部分出现。