# 示例代码:使用 replace() 函数替换字符串中的子字符串
# 原始字符串
original_string = "hello world"
# 使用 replace() 函数将 'world' 替换为 'Python'
new_string = original_string.replace("world", "Python")
# 输出结果
print(new_string) # 输出: hello Python
# 解释说明:
# replace() 函数用于返回一个新的字符串,其中指定的子字符串被替换成新的子字符串。
# 语法:str.replace(old, new[, count])
# 参数:
# - old: 要替换的子字符串
# - new: 新的子字符串
# - count(可选): 替换的最大次数。如果不指定,则替换所有匹配项。
# 示例:指定替换次数
text = "one one was a race horse, two two was one too."
result = text.replace("one", "three", 1)
print(result) # 输出: three one was a race horse, two two was one too.
上一篇:python sort函数
下一篇:python input函数用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站