{"id":1102920,"date":"2025-01-08T16:04:56","date_gmt":"2025-01-08T08:04:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1102920.html"},"modified":"2025-01-08T16:05:00","modified_gmt":"2025-01-08T08:05:00","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102920.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u5339\u914d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065006\/4177e080-cc3c-4cf3-af0c-82583606ddac.webp\" alt=\"python\u4e2d\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5339\u914d\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3002<\/strong> \u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5339\u914d\u9700\u6c42\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong><\/h2>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u7528\u4e8e\u5339\u914d\u548c\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u672c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1\u3001find() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>find()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u5982\u679c\u672a\u627e\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de -1\u3002<\/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>if index != -1:<\/p>\n<p>    print(f&quot;&#39;Python&#39; found at index {index}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Python&#39; not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001index() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>index()<\/code> \u65b9\u6cd5\u4e0e <code>find()<\/code> \u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u672a\u627e\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u4f1a\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    index = text.index(&quot;Python&quot;)<\/p>\n<p>    print(f&quot;&#39;Python&#39; found at index {index}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;&#39;Python&#39; not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001startswith() \u548c endswith() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>startswith()<\/code> \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u5f00\u5934\uff0c\u800c <code>endswith()<\/code> \u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if text.startswith(&quot;Hello&quot;):<\/p>\n<p>    print(&quot;The string starts with &#39;Hello&#39;&quot;)<\/p>\n<p>if text.endswith(&quot;Python.&quot;):<\/p>\n<p>    print(&quot;The string ends with &#39;Python.&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001in \u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code> \u64cd\u4f5c\u7b26\u7528\u4e8e\u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if &quot;Python&quot; in text:<\/p>\n<p>    print(&quot;&#39;Python&#39; is in the text&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Python&#39; is not in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff0c\u7b80\u79f0 regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\u3002Python \u63d0\u4f9b\u4e86 <code>re<\/code> \u6a21\u5757\u7528\u4e8e\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001re \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165 <code>re<\/code> \u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001search() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>search()<\/code> \u65b9\u6cd5\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7684\u7b2c\u4e00\u6b21\u51fa\u73b0\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\u3002\u5982\u679c\u672a\u627e\u5230\uff0c\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;Python&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(f&quot;&#39;Python&#39; found at index {match.start()}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Python&#39; not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001match() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>match()<\/code> \u65b9\u6cd5\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u5f00\u59cb\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u5982\u679c\u672a\u627e\u5230\u5339\u914d\uff0c\u5219\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;Hello&quot;<\/p>\n<p>match = re.match(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(f&quot;&#39;Hello&#39; found at the beginning of the text&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Hello&#39; not found at the beginning&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001findall() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>findall()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;\\b\\w{5}\\b&quot;  # \u5339\u914d\u6240\u6709\u957f\u5ea6\u4e3a5\u7684\u5355\u8bcd<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(&quot;Words with length 5:&quot;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001sub() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>sub()<\/code> \u65b9\u6cd5\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;Python&quot;<\/p>\n<p>replacement = &quot;Java&quot;<\/p>\n<p>new_text = re.sub(pattern, replacement, text)<\/p>\n<p>print(&quot;Modified text:&quot;, new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u57fa\u4e8e\u7d22\u5f15\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u7840\u5207\u7247<\/h3>\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(&quot;Substring:&quot;, substring)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6b65\u957f\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u6b65\u957f\u5207\u7247\u7528\u4e8e\u4ee5\u56fa\u5b9a\u6b65\u957f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;abcdefg&quot;<\/p>\n<p>substring = text[::2]<\/p>\n<p>print(&quot;Substring with step 2:&quot;, substring)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u5339\u914d\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\u5e94\u7528\u573a\u666f\u3002<strong>\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u57fa\u672c\u9700\u6c42\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\u5f3a\u5927\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u590d\u6742\u5339\u914d\uff1b\u5b57\u7b26\u4e32\u5207\u7247\u9002\u7528\u4e8e\u5df2\u77e5\u4f4d\u7f6e\u7684\u5339\u914d\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u67e5\u627e\u8fd8\u662f\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\uff1f<\/strong><br \/>Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u5728\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u5339\u914d\u3002\u4f7f\u7528<code>re.search()<\/code>\u3001<code>re.match()<\/code>\u6216<code>re.findall()<\/code>\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u6a21\u5f0f\u67e5\u627e\u5339\u914d\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.findall(r&#39;\\d+&#39;, &#39;abc123def456&#39;)<\/code>\u53ef\u4ee5\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>in<\/code>\u5173\u952e\u5b57\u8f7b\u677e\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>if &#39;hello&#39; in &#39;hello world&#39;:<\/code>\u5c06\u8fd4\u56deTrue\uff0c\u8868\u793a&#39;hello&#39;\u786e\u5b9e\u662f&#39;hello world&#39;\u7684\u4e00\u90e8\u5206\u3002\u6b64\u5916\uff0c<code>str.find()<\/code>\u548c<code>str.index()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5b83\u4eec\u4f1a\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff0c<code>str.find()<\/code>\u5728\u672a\u627e\u5230\u65f6\u8fd4\u56de-1\uff0c\u800c<code>str.index()<\/code>\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u5339\u914d\uff1f<\/strong><br \/>\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d\uff0c\u901a\u8fc7\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\u6216\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7edf\u4e00\u7684\u5927\u5c0f\u5199\u5f62\u5f0f\u3002\u4f7f\u7528<code>re.search(&#39;pattern&#39;, &#39;String&#39;, re.IGNORECASE)<\/code>\u53ef\u4ee5\u5728\u4e0d\u8003\u8651\u5927\u5c0f\u5199\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u5339\u914d\u3002\u53e6\u5916\uff0c\u4f7f\u7528<code>str.lower()<\/code>\u6216<code>str.upper()<\/code>\u65b9\u6cd5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u540c\u4e00\u79cd\u5927\u5c0f\u5199\u540e\u8fdb\u884c\u6bd4\u8f83\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u505a\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5339\u914d\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1102927,"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\/1102920"}],"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=1102920"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102920\/revisions"}],"predecessor-version":[{"id":1102928,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102920\/revisions\/1102928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102927"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}