{"id":1078618,"date":"2025-01-08T12:13:18","date_gmt":"2025-01-08T04:13:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078618.html"},"modified":"2025-01-08T12:13:20","modified_gmt":"2025-01-08T04:13:20","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078618.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u67e5\u627e\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182052\/8b6c874a-ca63-4d76-8e61-523e04193b39.webp\" alt=\"python\u4e2d\u5982\u4f55\u67e5\u627e\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528find()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001in\u5173\u952e\u5b57\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528find()\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002find()\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u6b21\u51fa\u73b0\u4f4d\u7f6e\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u503c\u3002\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de-1\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u672c\u67e5\u627e\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>result = text.find(&quot;welcome&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001FIND()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u662fPython\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002\u5b83\u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u5b50\u5b57\u7b26\u4e32\u3001\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002\u5982\u679c\u5b50\u5b57\u7b26\u4e32\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\uff0c\u5219\u8fd4\u56de\u5176\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528find()\u65b9\u6cd5\u67e5\u627e\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>index = text.find(&quot;Python&quot;)<\/p>\n<p>print(index)  # \u8f93\u51fa\uff1a26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cfind()\u65b9\u6cd5\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;Python&quot;\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u503c26\u3002<\/p>\n<\/p>\n<p><h4>\u6307\u5b9a\u67e5\u627e\u8303\u56f4<\/h4>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u6765\u9650\u5b9a\u67e5\u627e\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>index = text.find(&quot;welcome&quot;, 0, 20)<\/p>\n<p>print(index)  # \u8f93\u51fa\uff1a7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfind()\u65b9\u6cd5\u5728\u6bcd\u5b57\u7b26\u4e32\u7684\u524d20\u4e2a\u5b57\u7b26\u4e2d\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;welcome&quot;\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\u503c7\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001INDEX()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u4e0efind()\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0cindex()\u65b9\u6cd5\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\uff0c\u800c\u4e0d\u662f\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528index()\u65b9\u6cd5\u67e5\u627e\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>try:<\/p>\n<p>    index = text.index(&quot;world&quot;)<\/p>\n<p>    print(index)  # \u8f93\u51fa\uff1a18<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cindex()\u65b9\u6cd5\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;world&quot;\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u503c18\u3002<\/p>\n<\/p>\n<p><h4>\u6307\u5b9a\u67e5\u627e\u8303\u56f4<\/h4>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u6765\u9650\u5b9a\u67e5\u627e\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>try:<\/p>\n<p>    index = text.index(&quot;world&quot;, 10, 30)<\/p>\n<p>    print(index)  # \u8f93\u51fa\uff1a18<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cindex()\u65b9\u6cd5\u5728\u6bcd\u5b57\u7b26\u4e32\u7684\u7b2c10\u5230\u7b2c30\u4e2a\u5b57\u7b26\u4e4b\u95f4\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;world&quot;\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\u503c18\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001IN\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>in\u5173\u952e\u5b57\u662fPython\u4e2d\u7528\u4e8e\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u8fd0\u7b97\u7b26\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff08True\u6216False\uff09\uff0c\u8868\u793a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528in\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728<\/h4>\n<\/p>\n<p><p>in\u5173\u952e\u5b57\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>if &quot;Python&quot; in text:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cin\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32&quot;Python&quot;\u662f\u5426\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528not in\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u4e0d\u5b58\u5728<\/h4>\n<\/p>\n<p><p>not in\u5173\u952e\u5b57\u7528\u4e8e\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u4e0d\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>if &quot;Java&quot; not in text:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cnot in\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32&quot;Java&quot;\u662f\u5426\u4e0d\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528re\u6a21\u5757\u67e5\u627e\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>re\u6a21\u5757\u63d0\u4f9b\u4e86search()\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002search()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>match = re.search(&quot;Python&quot;, text)<\/p>\n<p>if match:<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a{match.start()}&quot;)  # \u8f93\u51fa\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a26<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cre.search()\u65b9\u6cd5\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;Python&quot;\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5e76\u8fd4\u56de\u5176\u5f00\u59cb\u4f4d\u7f6e26\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528findall()\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879<\/h4>\n<\/p>\n<p><p>findall()\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u6bcd\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u5b50\u5b57\u7b26\u4e32\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python is fun. Python is powerful.&quot;<\/p>\n<p>matches = re.findall(&quot;Python&quot;, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;Python&#39;, &#39;Python&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cre.findall()\u65b9\u6cd5\u67e5\u627e\u6bcd\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u5b50\u5b57\u7b26\u4e32&quot;Python&quot;\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u9879\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528sub()\u65b9\u6cd5\u66ff\u6362\u5b50\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>sub()\u65b9\u6cd5\u7528\u4e8e\u66ff\u6362\u6bcd\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u5b50\u5b57\u7b26\u4e32\u7684\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python is fun. Python is powerful.&quot;<\/p>\n<p>new_text = re.sub(&quot;Python&quot;, &quot;Java&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aJava is fun. Java is powerful.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cre.sub()\u65b9\u6cd5\u5c06\u6bcd\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u5b50\u5b57\u7b26\u4e32&quot;Python&quot;\u7684\u9879\u66ff\u6362\u4e3a&quot;Java&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u901a\u8fc7\u7d22\u5f15\u8303\u56f4\u4ece\u6bcd\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u548c\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>substring = text[7:14]<\/p>\n<p>print(substring)  # \u8f93\u51fa\uff1awelcome<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u5207\u7247\u4ece\u6bcd\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7b2c7\u5230\u7b2c14\u4e2a\u5b57\u7b26\uff0c\u5f97\u5230\u5b50\u5b57\u7b26\u4e32&quot;welcome&quot;\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u67e5\u627e\u5b50\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u8fd8\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>start_index = 7<\/p>\n<p>end_index = 14<\/p>\n<p>substring = text[start_index:end_index]<\/p>\n<p>if substring == &quot;welcome&quot;:<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a{start_index}&quot;)  # \u8f93\u51fa\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a7<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5224\u65ad\u5176\u662f\u5426\u7b49\u4e8e\u76ee\u6807\u5b50\u5b57\u7b26\u4e32\u3002\u5982\u679c\u76f8\u7b49\uff0c\u5219\u8f93\u51fa\u5b50\u5b57\u7b26\u4e32\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u7ec4\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u5c06\u591a\u79cd\u5b57\u7b26\u4e32\u65b9\u6cd5\u7ec4\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u67e5\u627e\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528in\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\uff0c\u7136\u540e\u4f7f\u7528find()\u65b9\u6cd5\u83b7\u53d6\u5176\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u7ec4\u5408\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python.&quot;<\/p>\n<p>target = &quot;Python&quot;<\/p>\n<p>if target in text:<\/p>\n<p>    index = text.find(target)<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a{index}&quot;)  # \u8f93\u51fa\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a26<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u4f7f\u7528in\u5173\u952e\u5b57\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u4f7f\u7528find()\u65b9\u6cd5\u83b7\u53d6\u5176\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u7ec4\u5408\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u5b57\u7b26\u4e32\u65b9\u6cd5\u7ec4\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u67e5\u627e\u9700\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python is fun. Python is powerful.&quot;<\/p>\n<p>pattern = &quot;Python&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>if matches:<\/p>\n<p>    index = text.find(pattern)<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a{index}&quot;)  # \u8f93\u51fa\uff1a\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a0<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u4e0d\u5728\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u4f7f\u7528re.findall()\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\uff0c\u7136\u540e\u4f7f\u7528find()\u65b9\u6cd5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5b57\u7b26\u4e32\u67e5\u627e\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4e5f\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u5904\u7406\u4e0d\u540c\u89c4\u6a21\u7684\u6570\u636e\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u5dee\u5f02\u3002\u901a\u5e38\u6765\u8bf4\uff0cin\u5173\u952e\u5b57\u548cfind()\u65b9\u6cd5\u7684\u6027\u80fd\u8f83\u597d\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b\uff0c\u7528\u4e8e\u6bd4\u8f83in\u5173\u952e\u5b57\u548cfind()\u65b9\u6cd5\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>text = &quot;Python is fun. &quot; * 1000000<\/p>\n<p>target = &quot;Python&quot;<\/p>\n<h2><strong>\u6d4b\u8bd5in\u5173\u952e\u5b57\u7684\u6027\u80fd<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>if target in text:<\/p>\n<p>    print(&quot;\u5b50\u5b57\u7b26\u4e32\u5b58\u5728\u4e8e\u6bcd\u5b57\u7b26\u4e32\u4e2d&quot;)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;in\u5173\u952e\u5b57\u8017\u65f6\uff1a{end_time - start_time}\u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5find()\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>index = text.find(target)<\/p>\n<p>if index != -1:<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff1a{index}&quot;)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;find()\u65b9\u6cd5\u8017\u65f6\uff1a{end_time - start_time}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u91cd\u590d\u62fc\u63a5\u5b57\u7b26\u4e32\u751f\u6210\u4e00\u4e2a\u8f83\u5927\u7684\u6bcd\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5206\u522b\u6d4b\u8bd5in\u5173\u952e\u5b57\u548cfind()\u65b9\u6cd5\u7684\u6027\u80fd\u3002\u8f93\u51fa\u7ed3\u679c\u663e\u793a\u4e86\u4e24\u79cd\u65b9\u6cd5\u7684\u8017\u65f6\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ecfind()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001in\u5173\u952e\u5b57\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u5207\u7247\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u627e\u9700\u6c42\uff0cfind()\u65b9\u6cd5\u548cin\u5173\u952e\u5b57\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u67e5\u627e\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u5176\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u7684\u67e5\u627e\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u5b50\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u5b50\u4e32\u53ef\u4ee5\u4f7f\u7528<code>str.find()<\/code>\u6216<code>str.index()<\/code>\u65b9\u6cd5\u3002<code>find()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5b50\u4e32\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\uff0c\u800c<code>index()<\/code>\u65b9\u6cd5\u5728\u672a\u627e\u5230\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, welcome to Python programming.&quot;\nindex = text.find(&quot;Python&quot;)  # \u8fd4\u56de\u5b57\u7b26\u4e32&quot;Python&quot;\u7684\u8d77\u59cb\u7d22\u5f15\nprint(index)  # \u8f93\u51fa\u7ed3\u679c\u4e3a 18\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u9664\u4e86<code>find()<\/code>\u548c<code>index()<\/code>\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>str.count()<\/code>\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7edf\u8ba1\u67d0\u4e2a\u5b50\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u5b50\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Python is great. Python is popular.&quot;\ncount = text.count(&quot;Python&quot;)  # \u7edf\u8ba1&quot;Python&quot;\u51fa\u73b0\u7684\u6b21\u6570\nprint(count)  # \u8f93\u51fa\u7ed3\u679c\u4e3a 2\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5339\u914d\u9879\uff1f<\/strong><br \/>\u8981\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5339\u914d\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>findall()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;Python is great. I love Python programming.&quot;\nmatches = re.findall(&quot;Python&quot;, text)  # \u67e5\u627e\u6240\u6709&quot;Python&quot;\u7684\u5339\u914d\u9879\nprint(matches)  # \u8f93\u51fa\u7ed3\u679c\u4e3a [&#39;Python&#39;, &#39;Python&#39;]\n<\/code><\/pre>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u80fd\u591f\u6839\u636e\u590d\u6742\u7684\u6a21\u5f0f\u8fdb\u884c\u67e5\u627e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528find()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":1078627,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078618"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1078618"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078618\/revisions"}],"predecessor-version":[{"id":1078631,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078618\/revisions\/1078631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078627"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}