。
函数名:find_word_in_sentence
函数功能:在给定的句子中查找指定的单词,并返回该单词在句子中的位置。
函数参数:
函数返回值:
函数示例代码:
def find_word_in_sentence(sentence, word):
words = sentence.split()
for i, w in enumerate(words):
if w == word:
return i
return -1
函数使用示例:
sentence = "This is a sample sentence for testing."
word = "sample"
position = find_word_in_sentence(sentence, word)
if position != -1:
print(f"The word '{word}' is found at position {position} in the sentence.")
else:
print(f"The word '{word}' is not found in the sentence.")
函数说明:
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云