在 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_pattern在this_string中没有找到,它就会发生一个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查找this形式。 - 假如
this形式存在,它将回来一个新的字符串,其间 _一切_呈现的this形式被替换为由with_this参数指定的形式。 - 假如
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.:)"
现在让咱们用"Python" 替换一切呈现的"C++" ,像这样。
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()办法查找和替换形式或子串。
期望你喜欢这个教程。
很快就会在另一篇文章中见到我们。在那之前,祝我们学习愉快!
Bala Priya C
我阅读、写作和编码
假如这篇文章对你有协助,请把它发到推特上。
免费学习代码。freeCodeCamp的开源课程已经协助超过40,000人获得开发作业。开始吧

