在 Python 中处理字符串时,你可能需要在字符串中搜索模式,甚至用另一个子字符串替换部分字符串。
Python 有有用的字符串方法 find() 和 replace() 可以帮助我们执行这些字符串处理任务。
在本教程中,我们将通过示例代码了解这两种字符串方法。
Python 字符串的不变性
Python 中的字符串是不可变的。因此,我们不能直接修改字符串。
Python 字符串是遵循零索引的可迭代对象。长度为n的字符串的有效索引为0,1,2,..,(n-1)。
我们也可以使用负索引,其中最后一个元素位于索引-1处,即倒数第二个元素位于索引-2等。
例如,我们有 my_string 保存字符串 writer。让我们尝试通过将最后一个字符设置为 s 来将其更改为 writes。
my_string = "writer"
让我们尝试将最后一个字符分配给字母 s,如下所示。通过负索引,我们知道 my_string 中最后一个字符的索引是 -1。
my_string[-1]="s"
# Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-670491032ba6> in <module>()
1 my_string = "writer"
----> 2 my_string[-1]="s"
TypeError: 'str' object does not support item assignment如上面的代码片段所示,我们得到了一个 TypeError。这是因为字符串对象是不可变的,我们尝试执行的赋值是无效的。
但是,我们可能经常需要修改字符串。字符串方法可以帮助我们轻松地做到这一点。
字符串方法对现有字符串进行操作,并返回新的修改后的字符串,不修改现有字符串。
让我们转到下一节来了解 find() 和 replace() 方法。
如何使用 find() 在 Python 字符串中搜索模式
你可以使用 Python 的 find() 方法在字符串中搜索模式。一般语法如下所示。
<this_string>.find(<this_pattern>)我们要搜索的输入字符串由占位符 this_string 表示。我们要搜索的模式由占位符 this_pattern 给出。
现在让我们分析一下上面的语法。
find()方法在this_string中搜索this_pattern的出现。- 如果
this_pattern存在,它返回this_pattern第一次出现的起始索引。 - 如果
this_pattern没有出现在this_string中,则返回-1。
▶ 让我们看一些例子。
Python find() 方法示例
我们以字符串 "I enjoy coding in Python!" 为例。
my_string = "I enjoy coding in Python!"
my_string.find("Python")
# Output
18在上面的例子中,我们尝试在 my_string 中搜索 Python。find() 方法返回 18,即模式 Python 的起始索引。
为了验证返回的索引是否正确,我们可以检查 my_string[18]=="P" 的计算结果是否为 True。
▶ 现在,我们将尝试搜索字符串中不存在的子字符串。
my_string.find("JavaScript")
# Output
-1在此示例中,我们尝试搜索字符串中不存在的 JavaScript。如前所述, find() 方法返回了 -1。
如何使用 find() 在 Python 子字符串中搜索模式
我们还可以使用 find() 方法在某个子字符串或字符串片段中搜索模式,而不是整个字符串。
在这种情况下, find() 方法调用采用以下形式:
<this_string>.find(<this_pattern>, <start_index>,<end_index>)这与前面讨论的语法非常相似。
唯一的区别是模式的搜索不是在整个字符串上。它只是在 start_index:end_index 指定的字符串的一部分上。如何使用 index() 在 Python 字符串中搜索模式
要在字符串中搜索模式,我们不妨使用 index() 方法。方法调用采用如下所示的语法。
<this_string>.index(<this_pattern>)index() 方法的工作方式与 find() 方法的工作方式非常相似。
- 如果
this_pattern存在于this_string中,则index()方法返回this_pattern第一次出现的起始索引。 - 但是,如果在
this_string中找不到this_pattern,则会引发ValueError。
▶ 看一下示例。
Python index() 方法示例
我们使用之前示例中的字符串 my_string = "I enjoy coding in Python!"。
my_string.index("Python")
# Output
18
在这种情况下,输出与 find() 方法的输出相同。
如果我们搜索字符串中不存在的子字符串,则 index() 方法会引发 ValueError。这显示在下面的代码片段中。
my_string.index("JavaScript")
# Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-377f7c374e16> in <module>()
----> 1 my_string.index("JavaScript")
ValueError: substring not found在下一节中,我们将学习如何查找和替换字符串中的模式。
如何使用 replace() 在 Python 字符串中查找和替换模式
如果你需要在字符串中搜索某个模式,并将其替换为另一个模式,则可以使用 replace() 方法。一般语法如下所示:
<this_string>.replace(<this>,<with_this>)我们现在分析这个语法。
replace()方法在this_string中搜索此模式。- 如果此模式存在,则返回一个新字符串,其中所有出现的此模式都将替换为由
with_this参数指定的模式。 - 如果在
this_string中找不到此模式,则返回的字符串与this_string相同。
如果你只想替换特定数量的模式,而不是全部模式,怎么办?
在这种情况下,你可以在方法调用中添加一个可选参数,指定你要替换的模式出现的次数。
<this_string>.replace(<this>,<with_this>, n_occurrences)在上面显示的语法中, n_occurrences 确保在返回的字符串中只替换模式的前 n 个。
▶ 让我们看一些例子来理解 replace() 函数是如何工作的。
Python replace() 方法示例
我们重新定义 my_string:
I enjoy coding in C++.
C++ is easy to learn.
I've been coding in C++ for 2 years now.:)如下:
my_string = "I enjoy coding in C++.\nC++ is easy to learn.\nI've been coding in C++ for 2 years now.:)"
我们将全部 "C++" 替换为 "Python",如下:
my_string.replace("C++","Python")
# Output
'I enjoy coding in Python.\nPython is easy to learn.\nI've been coding in Python for 2 years now.:)'当 replace() 方法返回一个字符串时,我们会在输出中看到 \n 字符。对于引入换行符的 \n 字符,我们可以打印出如下所示的字符串:
print(my_string.replace("C++","Python"))
# Output
I enjoy coding in Python.
Python is easy to learn.
I've been coding in Python for 2 years now.:)在上面的例子中,我们看到所有 C++ 都被 Python 替换了。
▶ 现在,我们还将使用额外的 n_occurrences 参数。
以下代码返回一个字符串,其中前两次出现的 C++ 被替换为 Python。
print(my_string.replace("C++","Python",2))
# Output
I enjoy coding in Python.
Python is easy to learn.
I've been coding in C++ for 2 years now.:)以下代码返回一个字符串,其中只有第一次出现的 C++ 被替换为 Python:
print(my_string.replace("C++","Python",1))
# Output
I enjoy coding in Python.
C++ is easy to learn.
I've been coding in C++ for 2 years now.:)▶ 现在,我们尝试替换 my_string 中不存在的子字符串 JavaScript。 返回的字符串与 my_string 相同。
print(my_string.replace("JavaScript","Python"))
# Output
I enjoy coding in C++.
C++ is easy to learn.
I've been coding in C++ for 2 years now.:)总结
在本教程中,我们学习了以下内容:
- 如何使用
find()和index()方法在 Python 中搜索字符串,以及 - 如何使用
replace()方法查找和替换模式或子字符串
希望你喜欢这个教程。祝你学习愉快!
原文:Python String Methods Tutorial – How to Use find() and replace() on Python Strings,作者:Bala Priya C