.寻找字符串“I think hope is a good thing.”中字符串“hope”的下标,并将字符串中的“hope”替换成其他事物,将新字符串保存到变量 result 中并在屏幕上输出。
时间: 2024-03-20 16:43:11 浏览: 110
字符串子串寻找及替换
好的,以下是Python的实现代码:
```python
string = "I think hope is a good thing."
index = string.find('hope')
if index != -1:
result = string[:index] + 'love' + string[index+4:]
print(result)
else:
print("字符串中未找到'hope'")
```
输出结果为:`I think love is a good thing.`。
阅读全文