检查字符串“Life is short.I use python”中是否包含字符串“python”,若包含则替换为“python”后输出新字符串,否则输出原字符串
时间: 2023-12-24 07:05:59 浏览: 44
包含,输出新字符串“Life is short.I use python”。
代码如下:
```python
s = "Life is short.I use python"
if "python" in s:
s = s.replace("python", "python")
print(s)
else:
print(s)
```
相关问题
编写程序,检查字符串"Life is short.I use python"中是否包含字符串"python",若包含则替换为"Python"
是的,字符串"Life is short.I use python"包含字符串"python",可以使用Python代码将它替换为"Python"。以下是示例代码:
```python
string = "Life is short.I use python"
if "python" in string:
string = string.replace("python", "Python")
print(string)
```
输出结果是:"Life is short.I use Python"。
编写python程序,检查字符串" Life is short. I use python"中是否包含字符串"python",若包含则替换为"Python"后输出新字符串,否则输出原字符串。
可以使用Python中的字符串方法`find()`来检查字符串中是否包含另一个字符串,并返回其索引位置。如果返回-1,则表示未找到该字符串。根据这个方法,可以编写如下程序:
```python
string = "Life is short. I use python"
if string.find("python") != -1:
new_string = string.replace("python", "Python")
print(new_string)
else:
print(string)
```
输出结果为:
```
Life is short. I use Python
```