python 字符串的替换 replace

python 字符串的替换 replace

  1. 语法: my_str:replace(old_str, new_str, count)
  2. count: 可以不写,默认替换所有
  3. 将旧的字符串替换成新的字符串, 默认替换全换全部, 不会修改原来的字符串, 得到一个新的字符串
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
发布了56 篇原创文章 · 获赞 17 · 访问量 2179

猜你喜欢

转载自blog.csdn.net/LanlanDeming/article/details/103318355