1.C語言學習筆記:可使用方法replace()將字符串中的特定單詞都替換為另一個單詞。下面是一個簡單的示例,演示了如何將句子中的「dog」替換為"cat":
>>> message="I really like dogs."
>>> message.replace("dog","cat")
'I really like cats.'
讀取你剛創建的文件learning_python.txt中的每一行,將其中的Python都替換為另一門語言的名稱,如C。將修改後的每行都列印到屏幕上。
learning_python.txt中的內容為
In Python you can broaden your horizons.
In Python you can get joy beyond learning.
In Python you can understand the magic of computers.
代碼為:
with open("learning_python.txt") as object:
content=object.read()
content=content.replace("Python","C")
print(content)
結果為:
喜歡我的小夥伴,可以關注我的百家號,謝謝!