python 字符串函数
Python provides find()
function in string variable types in order to find the specified term or string. The find()
function will return the first occurrence of the given search term position in the given string. If the given search term is not found the find()
method will return -1
in order to express nothing found.
Python提供字符串变量类型的find()
函数,以查找指定的术语或字符串。 find()
函数将返回给定字符串中给定搜索词位置的首次出现。 如果find()
给定的搜索词,则find()
方法将返回-1
以表示未找到任何内容。
find()函数语法 (find() Function Syntax)
The find()
method has the following syntax. It can accept up to 3 parameters where two of them are optional which means they can be omitted.
find()
方法具有以下语法。 它最多可以接受3个参数,其中两个是可选参数,这意味着可以省略它们。
STRING.find(VALUE, START, END)
- `STRING` is the variable name where the text or string is stored which will be searched. “ STRING”是将要搜索的存储文本或字符串的变量名称。
- `VALUE` is the value, string or term which will be searched in the STRING. “ VALUE”是将在STRING中搜索的值,字符串或术语。
- `START` is optional and used to set the search start position in the STRING. “ START”是可选的,用于设置STRING中的搜索开始位置。
- `END` is optional and used to set the search end position in the STRING. END是可选的,用于设置STRING中的搜索结束位置。
查找给定的字符串 (Find Given String)
We will search with a simple example where we will search the given term in the given string. In this example we will search the termpoftut.com
in the string I love poftut.com
.
我们将通过一个简单的示例进行搜索,在该示例中,我们将在给定的字符串中搜索给定的术语。 在这个例子中,我们将搜索术语poftut.com
字符串中的I love poftut.com
。
str="I love poftut.com"
match = str.find("poftut.com")
print(match)
# The output will be 7
match = str.find("POFTUT.COM")
print(match)
# The output will be -1
We can see the start index of the poftut.com
is 7 in the I love poftut.com
where POFTUT.COM
can not be found in the given string. Because find()
function search in a case sensitive manner so the search will return -1
in order to express there is not match.
我们可以看到的起始索引poftut.com
是7在I love poftut.com
其中POFTUT.COM
不能给定的字符串中找到。 因为find()
函数以区分大小写的方式进行搜索,所以搜索将返回-1
以表示不匹配。
从指定位置查找字符串到结尾 (Find String From Specified Position To End)
Alternatively, we can specify the start position of the search where the given term will be searched from the given start position or index. The start position or index will be provided as the second argument to the find()
method after the search term. In this following example, we will search the term poftut.com
in the I love poftut.com
by starting the search from different index numbers.
或者,我们可以指定搜索的起始位置,在该位置将从给定的起始位置或索引中搜索给定的术语。 起始位置或索引将作为搜索项之后的find()
方法的第二个参数提供。 在下面的示例中,我们将从不同的索引号开始搜索,在I love poftut.com
搜索术语poftut.com
。
str="I love poftut.com"
match = str.find("poftut.com",3)
print(match)
#The output will be 7
match = str.find("poftut.com",9)
print(match)
#The output will be -1
从开始到指定位置查找字符串(Find String From Start To Specified Position)
In the previous example, we set the positions of the search. We can also set the end positions of the search. In this example, we will set 10 as the end index number of the search. We will also specify the start as 0 index number.
在前面的示例中,我们设置了搜索位置。 我们还可以设置搜索的结束位置。 在此示例中,我们将设置10作为搜索的结束索引号。 我们还将起始位置指定为0索引号。
str="I love poftut.com"
match = str.find("poftut.com",0,10)
print(match)
#The output will be -1
match = str.find("poftut.com",0,20)
print(match)
#The output will be 7
We can see that when we set the search end index as 10 the poftut.com
term can not be found. If we set the end index or position as 20 the poftut.com
found at index number 7.
我们可以看到,当我们将搜索结束索引设置为10时, poftut.com
字词。 如果我们将结束索引或位置设置为20,则poftut.com
的索引号为7。
在指定位置(开始和结束)之间查找字符串(Find String Between Specified Positions (Start and End))
We can explicitly specify the start and end index numbers for the search. We will provide them as second and third arguments to the find()
function.
我们可以明确指定搜索的开始和结束索引号。 我们将它们作为find()
函数的第二和第三个参数提供。
str="I love poftut.com"
match = str.find("poftut.com",3,20)
print(match)
7
match = str.find("poftut.com",12,20)
print(match)
-1
翻译自: https://www.poftut.com/python-string-find-function-tutorial-with-examples/
python 字符串函数