1. 使用字符串方法 `replace()`
```python
text = "Hello, world!"
new_text = text.replace("world", "Python")
print(new_text) 输出:Hello, Python!
2. 使用正则表达式模块 `re` 的 `sub()` 方法
```python
import re
text = "Hello, world!"
new_text = re.sub(r"world", "Python", text)
print(new_text) 输出:Hello, Python!
3. 使用正则表达式模块 `re` 的 `search()` 方法查找子字符串
```python
import re
text = "Hello, world!"
match = re.search(r"world", text)
if match:
print("Found:", match.group()) 输出:Found: world
else:
print("Not found")
4. 使用正则表达式模块 `re` 的 `findall()` 方法查找所有匹配的子字符串
```python
import re
text = "Hello, World!"
matches = re.findall(r"l", text)
print(matches) 输出:['l', 'l']
5. 使用正则表达式模块 `re` 的 `compile()` 方法预编译模式以提高性能
```python
import re
datepat = re.compile(r"(\d+)/(\d+)/(\d+)")
new_text = datepat.sub(r"\3-\1-\2", "11/28/2018")
print(new_text) 输出:2018-11-28
以上方法可以帮助你在Python中查找和替换字符串。如果你需要更复杂的查找和替换逻辑,比如条件替换或者基于模式的替换,你可能需要使用更高级的正则表达式技巧或者第三方库,如 `pandas`