當前位置:成語大全網 - 新華字典 - Python 如何替換text中第某個詞為指定詞?

Python 如何替換text中第某個詞為指定詞?

#?假設妳的?text?是壹個字符串?內容如下

text?=?"welcome?to?the?world?of?python?python?is?the?most?popular?script?language"

#?將?text?整句話切分為各個單詞,通過?split?函數

words?=?text.split()

#?替換第十個單詞為?python

words[9]?=?"python"

#?重新組合所有單詞為壹個新的句子?即?text

text?=?"?".join(words)

#?打印輸出?text

print(text)

希望對妳有所幫助~~~