
python
# 定义需要检查的字符串
original_string = "Life is short.I use python"
# 检查字符串中是否包含"python"
if "python" in original_string:
# 找到后进行替换
new_string = original_string.replace("python", "Python")
print("The string contains 'python' and has been replaced with 'Python'.")
print(new_string)
else:
print("The string does not contain 'python'.")
当运行上述代码时,程序会首先检查原始字符串“Life is short.I use python”中是否包含子字符串“python”。由于字符串中确实包含“python”,因此程序会执行替换操作,将“python”替换为“Python”,并打印出替换后的新字符串。
请注意,这个程序使用了Python的内置函数`in`来检查子字符串是否存在,以及`replace()`方法来进行替换操作。这两个函数都是处理字符串时常用的工具。
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv183532