日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

在Python中查找子字符串索引的5种方法

發布時間:2023/11/29 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Python中查找子字符串索引的5种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Python中查找字符串中子字符串索引的5種方法 (5 Ways to Find the Index of a Substring in Strings in Python)

  • str.find()

    str.find()

  • str.rfind()

    str.rfind()

  • str.index()

    str.index()

  • str.rindex()

    str.rindex()

  • re.search()

    re.search()

  • str.find() (str.find())

    str.find() returns the lowest index in the string where the substring sub is found within the slice s[start:end]. It returns -1 if the sub is not found.start and end are optional arguments.

    str.find()返回在切片s[start:end]找到子字符串sub的字符串中的最低索引。 如果找不到該子項,則返回-1 。 start和end是可選參數。

    str.find(sub,start,end)

    例子1.使用str.find()方法 (Example 1. Using str.find() method)

    Photo by the author作者照片

    The string is banana.

    線是banana 。

    The substring is an.

    該子是an 。

    The substring occurs two times in the string.

    子字符串在字符串中出現兩次。

    str.find(“an”) returns the lowest index of the substring an.

    str.find(“an”)返回子字符串an的最低索引。

    s1="banana"print (s1.find("an"))#Output:1

    例子2.使用str.find()方法和提到的start參數 (Example 2. Using str.find() method with the start parameter mentioned)

    The substring is an.

    該子是an 。

    The start parameter is 2. It will start searching the substring an from index 2.

    起始參數為2 。 它將開始從索引2搜索子字符串an 。

    s1="banana"print (s1.find("an",2))#Output:3

    例子3.如果沒有找到子字符串,它將返回-1 (Example 3. If the substring is not found, it will return -1)

    The substring is ba.

    子字符串是ba 。

    The start parameter is 1 and the stop parameter is 5. It will start searching the substring from index 1 to index 5 (excluded).

    起始參數為1 ,終止參數為5 。 它將開始搜索從索引1到索引5(不包括)的子字符串。

    Since the substring is not found in the string within the given index, it returns -1.

    由于在給定索引的字符串中找不到子字符串,因此它返回-1 。

    s1="banana"print (s1.find("ba",1,5))#Output:-1

    2. str.rfind() (2. str.rfind())

    str.rfind() returns the highest index in the string where the substring sub is found within the slice s[start:end]. It returns -1 if the sub is not found.start and end are optional arguments.

    str.rfind()返回在slice s[start:end]找到子字符串sub的字符串中的最高索引。 如果找不到該子項,則返回-1 。 start和end是可選參數。

    str.rfind(sub,start,end)

    例子1.使用str.rfind()方法 (Example 1. Using str.rfind() method)

    Photo by the author作者照片

    The string is banana.

    線是banana 。

    The substring is an.

    該子是an 。

    The substring occurs two times in the string.

    子字符串在字符串中出現兩次。

    str.find(“an”) returns the highest index of the substring an.

    str.find(“an”)返回子字符串an的最高索引。

    s1="banana"print (s1.rfind("an"))#Output:3

    例子2.使用str.rfind()方法并提到開始和結束參數 (Example 2. Using str.rfind() method with the start and end parameters mentioned)

    The substring is an.

    該子是an 。

    The start and end parameters are 1 and 4, respectively. It will start searching the substring from index 1 and index 4 (excluded).

    start和end參數分別為1和4 。 它將開始從索引1和索引4(排除)中搜索子字符串。

    s1="banana"print (s1.rfind("an",1,4))#Output:1

    例子3.如果沒有找到子字符串,它將返回-1 (Example 3. If the substring is not found, it will return -1)

    The substring is no.

    子字符串為no 。

    Since the substring is not found in the string, it returns -1.

    由于在字符串中找不到子字符串,因此它返回-1 。

    s1="banana"print (s1.rfind("no"))#Output:-1

    3. str.index() (3. str.index())

    Similarly to find(), str.index() returns the lowest index of the substring found in the string. It raises a ValueError when the substring is not found.

    與find()類似, str.index()返回 字符串中找到的子字符串的最低索引。 當找不到子字符串時,它將引發ValueError 。

    例子1.使用str.index()方法 (Example 1. Using str.index() method)

    s1="banana"print (s1.index("an"))#Output:1

    例子2.在給定start和end參數的情況下使用str.index()方法 (Example 2. Using str.index() method with the start and end parameters given)

    s1="banana"print (s1.index("an",2,6))#Output:3

    例子3.如果沒有找到子字符串,它將引發一個ValueError (Example 3. If the substring is not found, it raises a ValueError)

    s1="banana"print (s1.index("no"))#Output:ValueError: substring not found

    4. str.rindex() (4. str.rindex())

    Similarly to find(), str.rindex() returns the highest index of the substring found in the string. It raises a ValueError when the substring is not found.

    與find()類似, str.rindex()返回在字符串中找到的子字符串的最高索引。 當找不到子字符串時,它將引發ValueError 。

    例子1.使用str.rindex()方法 (Example 1. Using str.rindex() method)

    s1="banana"print (s1.rindex("an"))#Output:3

    例子2.在給定start和end參數的情況下使用str.index()方法 (Example 2. Using str.index() method with the start and end parameters given)

    s1="banana"print (s1.rindex("an",0,4))#Output:1

    例子3.如果沒有找到子字符串,它將引發一個ValueError (Example 3. If the substring is not found, it raises a ValueError)

    s1="banana"print (s1.rindex("no"))#Output:ValueError: substring not found

    5. re.search() (5. re.search())

    re.search(pattern, string, flags=0)

    “Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.” — Python’s official documentation

    “掃描字符串以查找正則表達式模式產生匹配項的第一個位置,然后返回相應的匹配對象。 如果字符串中沒有位置與模式匹配,則返回None否則,返回None 。 請注意,這不同于在字符串中的某個位置找到零長度匹配。” — Python的官方文檔

    • re.search (pattern, string): We have to mention the pattern to be searched in the string.

      re.search (模式,字符串):我們不得不提一下pattern中要搜索string 。

    • The return type matches the object that contains the starting and ending index of that pattern (substring).

      返回類型與包含該模式(子字符串)的開始和結束索引的對象匹配。
    • We can find the start and end indices from the match object using match.start() and match.end().

      我們可以使用match.start()和match.end()從match對象中找到start和end索引。

    Match.start([group])Match.end([group])

    “Return the indices of the start and end of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match.” — Python’s documentation

    “返回分組匹配的子字符串的開始和結束的索引; 組默認為零(表示整個匹配的子字符串)。 如果組存在但沒有參與比賽,則返回-1 ?!?— Python的文檔

    • We can get the start and end indices in tuple format using match.span().

      我們可以使用match.span()獲得元組格式的start和end索引。

    Match.span([group])

    “For a match m, return the 2-tuple (m.start(group), m.end(group)). Note that if group did not contribute to the match, this is (-1, -1). group defaults to zero, the entire match.” — Python’s documentation

    “對于匹配項m ,返回2元組(m.start(group), m.end(group)) 。 請注意,如果group對匹配沒有貢獻,則為(-1, -1) 。 組默認為零,即整個匹配。” — Python的文檔

    例子1.使用re.search() (Example 1. Using re.search())

    示例2.如果在字符串中未找到子字符串,則返回None (Example 2. If a substring is not found in the string, it returns None)

    import re
    string = 'banana'pattern = 'no'match=(re.search(pattern, string))#Returns match objectprint (match)#Output: None

    結論 (Conclusion)

    • Python 3.8.1 is used.

      使用Python 3.8.1。
    • str.find(), str.rfind() — Returns -1 when a substring is not found.

      str.find()和str.rfind() —在找不到子字符串時返回-1 。

    • str.index(),str.rindex() — Raises a ValueError when a substring is not found.

      str.index() , str.rindex() —在找不到子字符串時引發ValueError 。

    • re.search() — Returns None when a substring is not found.

      re.search() —如果找不到子字符串,則返回None 。

    • str.find(), str,index() — Returns the lowest index of the substring.

      str.find() , str,index() —返回子字符串的最低索引。

    • str.rfind(), str.rindex() — Returns the highest index of the substring.

      str.rfind() , str.rindex() —返回子字符串的最高索引。

    • re.search() — Returns the match object that contains the starting and ending indices of the substring.

      re.search() —返回包含子字符串的開始和結束索引的匹配對象。

    資源(Python文檔) (Resources (Python Documentation))

    • str.find

      查找

    • str.index

      指數

    • str.rfind

      查找

    • str.rindex

      索引

    • re.search

      研究

    • match-objects

      匹配對象

    翻譯自: https://medium.com/better-programming/5-ways-to-find-the-index-of-a-substring-in-python-13d5293fc76d

    總結

    以上是生活随笔為你收集整理的在Python中查找子字符串索引的5种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。