原文:Python String.Replace() – Function in Python for Substring Substitution,作者:Dionysia Lemonaki
在本文中,你将了解如何使用 Python 的 .replace()
方法来执行子字符串替换。
你还将看到如何执行不区分大小写的子字符串替换。
让我们开始吧!
Python 的 .replace() 方法有什么作用?
使用 .replace()
方法时,你可以用一个新字符替换一个特定字符的每个实例。你甚至可以用你指定的新文本行替换整个文本字符串。
.replace()
方法返回一个字符串的副本。这意味着旧的子字符串保持不变,但会创建一个新副本——所有旧文本都已被新文本替换。
Python 的 .replace() 方法是如何运行的?
.replace()
方法的语法如下所示:
string.replace(old_text, new_text, count)
让我们分解一下:
old_text
是.replace()
接受的第一个必需参数。这是你要替换的旧字符或文本。将此用引号括起来。new_text
是.replace()
接受的第二个必需参数。这是你要替换旧字符/文本的新字符或文本。该参数也需要用引号引起来。count
是.replace()
接受的第三个参数,是可选的。默认情况下,.replace()
将替换子字符串的所有实例。但是,你可以使用count
来指定要替换的出现次数。
Python .replace()
方法代码示例
如何替换单个字符的所有实例
要更改单个字符的所有实例,你可以执行以下操作:
phrase = "I like to learn coding on the go"
# 用 'a' 替换 'o' 的所有实例
substituted_phrase = phrase.replace("o", "a" )
print(phrase)
print(substituted_phrase)
# 输出
#I like to learn coding on the go
#I like ta learn cading an the ga
在上面的示例中,包含字符 o
的每个单词,其中的 o
都被替换为字符 a
。
在这个例子中,有四个字符 o
的实例。具体来说,它出现在 to
、coding
、on
和 go
中。
如果你只想将两个单词(例如 to
和 coding
)中的 o
更改为包含 a
,该怎么办?
如何仅替换单个字符的一定数量的实例
要仅更改单个字符的两个实例,你可以使用 count
参数并将其设置为 2:
phrase = "I like to learn coding on the go"
# 用 'a' 替换 'o' 的两个实例
substituted_phrase = phrase.replace("o", "a", 2 )
print(phrase)
print(substituted_phrase)
# 输出
#I like to learn coding on the go
#I like ta learn cading on the go
如果你只想更改单个字符的第一个实例,你可以将 count
参数设置为 1:
phrase = "I like to learn coding on the go"
# 用 'a' 替换 'o' 的一个实例
substituted_phrase = phrase.replace("o", "a", 1 )
print(phrase)
print(substituted_phrase)
# 输出
#I like to learn coding on the go
#I like ta learn coding on the go
如何替换字符串的所有实例
要更改多个字符,过程看起来相似。
phrase = "The sun is strong today. I don't really like sun."
# 用 `wind` 替换 `sun` 的所有实例
substituted_phrase = phrase.replace("sun", "wind")
print(phrase)
print(substituted_phrase)
# 输出
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like wind.
在上面的示例中,单词 sun
被替换为单词 wind
。
如何只替换一定数量的字符串实例
如果你只想将 sun
的第一个实例更改为 wind
,你可以使用 count
参数并将其设置为 1。
phrase = "The sun is strong today. I don't really like sun."
# 用 `wind` 替换 `sun` 的第一个实例
substituted_phrase = phrase.replace("sun", "wind", 1)
print(phrase)
print(substituted_phrase)
# 输出
#The sun is strong today. I don't really like sun.
#The wind is strong today. I don't really like sun.
如何在 Python 中执行不区分大小写的子字符串替换
让我们看另一个例子。
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
#replace the text "Ruby" with "Python"
substituted_text = phrase.replace("Ruby", "Python")
print(substituted_text)
#output
#I am learning Python. I really enjoy the ruby programming language!
在这种情况下,我真正想做的是用 Python 替换单词 Ruby
的所有实例。
但是,有一个包含小写字母 r
的单词 ruby
,我也想更改它。
因为第一个字母是小写的,而不是我在 Ruby
中指定的大写,所以它保持不变并且没有更改为 Python。
.replace()
方法区分大小写,因此它执行区分大小写的子字符串替换。
为了执行不区分大小写的子字符串替换,你必须做一些不同的事情。
您需要使用 re.sub() 函数并使用 re.IGNORECASE 标志。
要使用 re.sub()
,你需要:
- 通过
import re
使用re
模块 - 指定正则表达式模式
- 提及你要替换模式的内容
- 提及你要对其执行此操作的字符串
- (可选)指定
count
参数以使替换更精确,并指定要进行的最大替换次数 re.IGNORECASE
标志告诉正则表达式执行不区分大小写的匹配
所以,全部的语法看起来像这样:
import re
re.sub(pattern, replace, string, count, flags)
以前面的例子为例:
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
这就是我用 Python 替换 Ruby
和 ruby
的方法:
import re
phrase = "I am learning Ruby. I really enjoy the ruby programming language!"
phrase = re.sub("Ruby","Python", phrase, flags=re.IGNORECASE)
print(phrase)
# 输出
#I am learning Python. I really enjoy the Python programming language!
总结
就是这些啦!你现在知道了子字符串替换的基础知识。希望本指南对你有所帮助。
要了解有关 Python 的更多信息,请查看 freeCodeCamp 的 Python 和科学计算认证课程。
你将从基础开始,学习对初学者友好的交互式课程。最后,你还将构建五个项目以巩固你所学的知识。
感谢你阅读本文,祝你编程愉快!