在Python中,文本查找和替換的常用操作
1. 使用字符串方法進行查找和替換 Python的字符串類 (str) 提供了簡單的查找和替換方法,如 find()、replace() 等。 示例: text = "Hello, world!" # 查找子字符串的位置 position = text.find("world") print(position) # 輸出: 7 # 替換子字符串 new_text = text.replace(
1. 使用字符串方法進行查找和替換 Python的字符串類 (str) 提供了簡單的查找和替換方法,如 find()、replace() 等。 示例: text = "Hello, world!" # 查找子字符串的位置 position = text.find("world") print(position) # 輸出: 7 # 替換子字符串 new_text = text.replace(