返回字符串的副本,其中所有出现的子字符串old被new替换。如果给出了可选的参数count,则仅替换第一个出现的计数。
replace(old, new[, count])
old - 将被替换的旧字符串。
new - 新字符串将替换旧字符串。
count - 处理替换的次数。
返回字符串
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-replace-method.html
来源:LearnFk无涯教程网
无涯教程来看一些 replace()方法的示例,以了解其功能。
# Python replace() method example # 变量声明 str = "Java is a programming language" # 调用函数 str2 = str.replace("Java","C") # 显示结果 print("Old String: \n",str) print("New String: \n",str2)
输出:
Old String: Java is a programming language New String: C is a programming language
# Python replace() method example # 变量声明 str = "Java C C# Java Php Python Java" # 调用函数 str2 = str.replace("Java","C#") # replaces all the occurences # 显示结果 print("Old String: \n",str) print("New String: \n",str2) # 调用函数 str2 = str.replace("Java","C#",1) # replaces first occurance only # 调用函数 print("\n Old String: \n",str) print("New String: \n",str2)
输出:
Old String: Java C C# Java Php Python Java New String: C# C C# C# Php Python C# Old String: Java C C# Java Php Python Java New String: C# C C# Java Php Python Java
# Python replace() method example # 变量声明 str = "Apple is a fruit" # 调用函数 str2 = str.replace(str,"Tomato is also a fruit") # 显示结果 print(str2)
输出:
Tomato is also a fruit
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕