python 字符串的替换 replace
- 语法: my_str:replace(old_str, new_str, count)
- count: 可以不写,默认替换所有
- 将旧的字符串替换成新的字符串, 默认替换全换全部, 不会修改原来的字符串, 得到一个新的字符串
my_str = "hello world itcast and itcastcpp"
my_str1 = my_str.replace("itcast", "itheima")
print("my_str ", my_str)
print("my_str1", my_str1)
my_str2 = my_str.replace("itcast", "itheima", 1)
print(my_str2)
# 输出结果是
my_str hello world itcast and itcastcpp
my_str1 hello world itheima and itheimacpp
hello world itheima and itcastcpp