Python String.Replace() - Python中用于子串替换的函数

352 阅读5分钟

在这篇文章中,你将看到如何使用Python的.replace() 方法来执行子串替换。

你还将看到如何执行不区分大小写的子串替换。

让我们开始吧!

.replace() Python方法是做什么的?

当使用.replace() Python 方法时,你可以用一个新的字符来替换一个特定字符的每个实例。你甚至可以用你指定的一行新的文本来替换一整串文本。

.replace() 方法返回一个字符串的副本。这意味着旧的子串保持不变,但会创建一个新的副本--所有的旧文本都被新文本所取代。

.replace() Python方法是如何工作的?语法分解

.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"

# replace all instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a" )

print(phrase)
print(substituted_phrase)

#output

#I like to learn coding on the go
#I like ta learn cading an the ga

在上面的例子中,包含字符o 的每个单词都被替换为字符a

在这个例子中,有四个字符o 的实例。具体来说,它出现在to,coding,on, 和go 这几个词中。

如果你只想改变两个词,如tocoding ,使之包含a 而不是o 呢?

如何只替换单个字符的一定数量的实例

要想只改变一个字符的两个实例,你可以使用count 参数并将其设置为两个:

phrase = "I like to learn coding on the go"

# replace only the first two instances of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 2 )

print(phrase)
print(substituted_phrase)

#output

#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"

# replace only the first instance of 'o' with 'a'
substituted_phrase = phrase.replace("o", "a", 1 )

print(phrase)
print(substituted_phrase)

#output

#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."

#replace all instances of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind")

print(phrase)
print(substituted_phrase)

#output

#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."

#replace only the first instance of the word 'sun' with 'wind'
substituted_phrase = phrase.replace("sun", "wind", 1)

print(phrase)
print(substituted_phrase)

#output

#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 这个词的所有实例。

然而,有一个单词ruby ,它的小写字母是r ,我也想改变它。

因为第一个字母是小写的,而不是像我在Ruby 中指定的大写字母,所以它保持不变,没有改变为Python

.replace() 方法是区分大小写的,因此它执行的是区分大小写的子串替换。

为了执行不区分大小写的子串替换,你必须做一些不同的事情。

你需要使用re.sub() 函数并使用re.IGNORECASE 标志。

要使用re.sub() ,你需要:

  • 使用re 模块,通过import re
  • 用一个正则表达式pattern
  • 提到你想用什么replace 的模式。
  • 提及你想执行此操作的string
  • 可以选择指定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 替换Rubyruby

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)

#output

#I am learning Python. I really enjoy the Python programming language!

结束语

就这样,你现在知道了子串替换的基本知识。希望本指南对你有所帮助。

要学习更多关于Python的知识,请查看freeCodeCamp的Python科学计算认证

你将从基础知识开始,以一种相互交流和适合初学者的方式学习。你还会在最后建立五个项目,将其付诸实践并帮助巩固你所学的知识。

谢谢你的阅读,祝你编码愉快