{"id":1102286,"date":"2025-01-08T15:58:24","date_gmt":"2025-01-08T07:58:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1102286.html"},"modified":"2025-01-08T15:58:27","modified_gmt":"2025-01-08T07:58:27","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e5%af%b9%e5%ba%94%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102286.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u67e5\u627e\u5bf9\u5e94\u7684\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\/25064654\/877f6b35-4a4e-4b66-a0b1-2be46c5e8c17.webp\" alt=\"python\u4e2d\u5982\u4f55\u67e5\u627e\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528find()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001str.cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns()\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\uff0c\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u67e5\u627e\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528find()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002\u5b83\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;Python&quot;<\/p>\n<p>position = text.find(substring)<\/p>\n<p>print(f&quot;The position of &#39;{substring}&#39; in the text is: {position}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528start\u548cend\u53c2\u6570<\/h4>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7start\u548cend\u53c2\u6570\uff0c\u6307\u5b9a\u67e5\u627e\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;o&quot;<\/p>\n<p>position = text.find(substring, 10, 20)<\/p>\n<p>print(f&quot;The position of &#39;{substring}&#39; in the text from index 10 to 20 is: {position}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528index()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u4e0efind()\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0cindex()\u65b9\u6cd5\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;Python&quot;<\/p>\n<p>try:<\/p>\n<p>    position = text.index(substring)<\/p>\n<p>    print(f&quot;The position of &#39;{substring}&#39; in the text is: {position}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;&#39;{substring}&#39; not found in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528start\u548cend\u53c2\u6570<\/h4>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u63a5\u53d7start\u548cend\u53c2\u6570\uff0c\u6307\u5b9a\u67e5\u627e\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;o&quot;<\/p>\n<p>try:<\/p>\n<p>    position = text.index(substring, 10, 20)<\/p>\n<p>    print(f&quot;The position of &#39;{substring}&#39; in the text from index 10 to 20 is: {position}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;&#39;{substring}&#39; not found in the text from index 10 to 20&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u67e5\u627e\u548c\u5339\u914d\u64cd\u4f5c\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>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\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>pattern = re.compile(r&quot;Python&quot;)<\/p>\n<p>match = pattern.search(text)<\/p>\n<p>if match:<\/p>\n<p>    print(f&quot;The position of &#39;Python&#39; in the text is: {match.start()}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Python&#39; not found in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u67e5\u627e\u6240\u6709\u5339\u914d\u9879<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, welcome to the world of Python. Python is great!&quot;<\/p>\n<p>pattern = re.compile(r&quot;Python&quot;)<\/p>\n<p>matches = pattern.findall(text)<\/p>\n<p>print(f&quot;Found {len(matches)} occurrences of &#39;Python&#39; in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528str.contains()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u65f6\uff0cPandas\u5e93\u4e2d\u7684str.contains()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u67e5\u627e\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([&quot;Hello, welcome to the world of Python&quot;, &quot;Python is great!&quot;, &quot;Hello, world!&quot;])<\/p>\n<p>contains_python = data.str.contains(&quot;Python&quot;)<\/p>\n<p>print(contains_python)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528in\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>in\u5173\u952e\u5b57\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\uff0c\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;Python&quot;<\/p>\n<p>if substring in text:<\/p>\n<p>    print(f&quot;&#39;{substring}&#39; is found in the text&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;&#39;{substring}&#39; is not found in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u8fdb\u884c\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\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(f&quot;The substring from index 7 to 14 is: &#39;{substring}&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002<strong>find()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001str.contains()\u65b9\u6cd5\u3001in\u5173\u952e\u5b57\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/strong>\u90fd\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7279\u70b9\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u67e5\u627e\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u5b57\u7b26\u4e32\u7684\u590d\u6742\u5ea6\u548c\u67e5\u627e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u627e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>find()\u65b9\u6cd5\u6216in\u5173\u952e\u5b57<\/strong>\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1b\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>Pandas\u5e93\u4e2d\u7684str.contains()\u65b9\u6cd5<\/strong>\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u5b57\u7b26\u4e32\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u67e5\u627e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u3002\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>str.find()<\/code>\u548c<code>str.index()<\/code>\u65b9\u6cd5\u3002<code>find()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u6700\u4f4e\u7d22\u5f15\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5219\u8fd4\u56de-1\uff0c\u800c<code>index()<\/code>\u65b9\u6cd5\u5728\u627e\u4e0d\u5230\u65f6\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>ValueError<\/code>\u3002\u53e6\u5916\uff0c\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u4e5f\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u67e5\u627e\u5927\u578b\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5bf9\u4e8e\u8f83\u5927\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re<\/code>\u6a21\u5757\uff09\u53ef\u4ee5\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u67e5\u627e\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>re.search()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u4f7f\u5f97\u5339\u914d\u590d\u6742\u6a21\u5f0f\u6210\u4e3a\u53ef\u80fd\uff0c\u5982\u5ffd\u7565\u5927\u5c0f\u5199\u6216\u5339\u914d\u591a\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u67e5\u627e\u4e2d\u7684\u5927\u5c0f\u5199\u654f\u611f\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u67e5\u627e\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u67e5\u627e\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\uff0c\u518d\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.lower()<\/code>\u6216<code>str.upper()<\/code>\u65b9\u6cd5\u6765\u7edf\u4e00\u5927\u5c0f\u5199\u540e\u518d\u8fdb\u884c\u67e5\u627e\u3002\u5bf9\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\u6765\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u627e\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528find()\u65b9\u6cd5\u3001index()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001str.contAI [&hellip;]","protected":false},"author":3,"featured_media":1102293,"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\/1102286"}],"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=1102286"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102286\/revisions"}],"predecessor-version":[{"id":1102296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102286\/revisions\/1102296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102293"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}