{"id":1163881,"date":"2025-01-15T15:00:13","date_gmt":"2025-01-15T07:00:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163881.html"},"modified":"2025-01-15T15:00:16","modified_gmt":"2025-01-15T07:00:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e7%89%b9%e5%ae%9a%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163881.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u627e\u5230\u7279\u5b9a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204542\/8ff087fb-5ca7-41a5-a519-2c32422c2131.webp\" alt=\"python\u4e2d\u5982\u4f55\u627e\u5230\u7279\u5b9a\u5b57\u7b26\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u627e\u5230\u7279\u5b9a\u5b57\u7b26\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982find()\u548cindex()\uff09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08\u5982re\u6a21\u5757\uff09\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u4e86find()\u548cindex()\u4e24\u4e2a\u65b9\u6cd5\u6765\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002find()\u65b9\u6cd5\u8fd4\u56de\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\uff1b\u800cindex()\u65b9\u6cd5\u5219\u5728\u672a\u627e\u5230\u65f6\u629b\u51faValueError\u5f02\u5e38\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<h2><strong>\u4f7f\u7528find()\u65b9\u6cd5<\/strong><\/h2>\n<p>position = text.find(char)<\/p>\n<p>if position != -1:<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u672a\u627e\u5230&quot;)<\/p>\n<h2><strong>\u4f7f\u7528index()\u65b9\u6cd5<\/strong><\/h2>\n<p>try:<\/p>\n<p>    position = text.index(char)<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u672a\u627e\u5230&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Python\u4e2d\u627e\u5230\u7279\u5b9a\u5b57\u7b26\u7684\u4e0d\u540c\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001find()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>find()\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u4e2d\u6700\u5e38\u7528\u7684\u67e5\u627e\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u6307\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.find(sub[, start[, end]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>sub<\/code>\u662f\u8981\u67e5\u627e\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>start<\/code>\u548c<code>end<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u5206\u522b\u8868\u793a\u67e5\u627e\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<h2><strong>\u67e5\u627e\u5b57\u7b26&#39;o&#39;\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/strong><\/h2>\n<p>position = text.find(char)<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<h2><strong>\u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;World&quot;\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/strong><\/h2>\n<p>substring = &quot;World&quot;<\/p>\n<p>position = text.find(substring)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<h2><strong>\u6307\u5b9a\u67e5\u627e\u8303\u56f4<\/strong><\/h2>\n<p>position = text.find(char, 5, 10)<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}\uff0c\u67e5\u627e\u8303\u56f4\u4e3a5\u523010&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001index()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u4e0efind()\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5728\u672a\u627e\u5230\u6307\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u65f6\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.index(sub[, start[, end]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u67e5\u627e\u5b57\u7b26&#39;o&#39;\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/p>\n<p>    position = text.index(char)<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p>    # \u67e5\u627e\u5b50\u5b57\u7b26\u4e32&quot;World&quot;\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e<\/p>\n<p>    substring = &quot;World&quot;<\/p>\n<p>    position = text.index(substring)<\/p>\n<p>    print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p>    # \u6307\u5b9a\u67e5\u627e\u8303\u56f4<\/p>\n<p>    position = text.index(char, 5, 10)<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}\uff0c\u67e5\u627e\u8303\u56f4\u4e3a5\u523010&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u672a\u627e\u5230&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08re\u6a21\u5757\uff09<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u67e5\u627e\u3002\u5728Python\u4e2d\uff0cre\u6a21\u5757\u63d0\u4f9b\u4e86\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u529f\u80fd\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u6216\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001search()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>search()\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56deNone\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">re.search(pattern, string, flags=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World!&quot;<\/p>\n<p>pattern = &#39;o&#39;<\/p>\n<h2><strong>\u4f7f\u7528search()\u65b9\u6cd5\u67e5\u627e\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6a21\u5f0f<\/strong><\/h2>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(f&quot;\u5339\u914d\u7684\u5b57\u7b26&#39;{pattern}&#39;\u5728\u7d22\u5f15\u4f4d\u7f6e\uff1a{match.start()}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u5339\u914d\u7684\u5b57\u7b26&#39;{pattern}&#39;\u672a\u627e\u5230&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001findall()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>findall()\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">re.findall(pattern, string, flags=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>pattern = &#39;o&#39;<\/p>\n<h2><strong>\u4f7f\u7528findall()\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u6a21\u5f0f<\/strong><\/h2>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(f&quot;\u5339\u914d\u7684\u5b57\u7b26&#39;{pattern}&#39;\u51fa\u73b0\u6b21\u6570\uff1a{len(matches)}&quot;)<\/p>\n<p>print(f&quot;\u5339\u914d\u7684\u5b57\u7b26&#39;{pattern}&#39;\u4f4d\u7f6e\uff1a{[m.start() for m in re.finditer(pattern, text)]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u904d\u5386\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u57fa\u672c\u4f46\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u67e5\u627e\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u627e\u5230\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u7684\u6240\u6709\u51fa\u73b0\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u904d\u5386\u5b57\u7b26\u4e32\u67e5\u627e\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<p>positions = []<\/p>\n<p>for i, c in enumerate(text):<\/p>\n<p>    if c == char:<\/p>\n<p>        positions.append(i)<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u904d\u5386\u5b57\u7b26\u4e32\u67e5\u627e\u5b50\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>substring = &quot;Hello&quot;<\/p>\n<p>positions = []<\/p>\n<p>for i in range(len(text) - len(substring) + 1):<\/p>\n<p>    if text[i:i+len(substring)] == substring:<\/p>\n<p>        positions.append(i)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5185\u7f6e\u65b9\u6cd5\u548c\u9ad8\u7ea7\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u65b9\u6cd5\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u5185\u7f6e\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u5b57\u7b26\u4e32\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001rfind()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>rfind()\u65b9\u6cd5\u7c7b\u4f3c\u4e8efind()\u65b9\u6cd5\uff0c\u4f46\u5b83\u4ece\u5b57\u7b26\u4e32\u7684\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.rfind(sub[, start[, end]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<h2><strong>\u4f7f\u7528rfind()\u65b9\u6cd5\u4ece\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u5b57\u7b26&#39;o&#39;<\/strong><\/h2>\n<p>position = text.rfind(char)<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u4ece\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001rindex()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>rindex()\u65b9\u6cd5\u7c7b\u4f3c\u4e8eindex()\u65b9\u6cd5\uff0c\u4f46\u5b83\u4ece\u5b57\u7b26\u4e32\u7684\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.rindex(sub[, start[, end]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char = &#39;o&#39;<\/p>\n<p>try:<\/p>\n<p>    # \u4f7f\u7528rindex()\u65b9\u6cd5\u4ece\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u5b57\u7b26&#39;o&#39;<\/p>\n<p>    position = text.rindex(char)<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u4ece\u53f3\u8fb9\u5f00\u59cb\u67e5\u627e\u7684\u7d22\u5f15\u4f4d\u7f6e\uff1a{position}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u672a\u627e\u5230&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001count()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>count()\u65b9\u6cd5\u7528\u4e8e\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002\u8be5\u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.count(sub[, start[, end]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>substring = &quot;Hello&quot;<\/p>\n<h2><strong>\u4f7f\u7528count()\u65b9\u6cd5\u7edf\u8ba1\u5b50\u5b57\u7b26\u4e32&quot;Hello&quot;\u7684\u51fa\u73b0\u6b21\u6570<\/strong><\/h2>\n<p>count = text.count(substring)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u51fa\u73b0\u6b21\u6570\uff1a{count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u6280\u5de7\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u67e5\u627e\u548c\u5904\u7406\u3002\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\u5e76\u8fdb\u884c\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>substring = &quot;Hello&quot;<\/p>\n<p>positions = []<\/p>\n<p>start = 0<\/p>\n<p>while True:<\/p>\n<p>    start = text.find(substring, start)<\/p>\n<p>    if start == -1:<\/p>\n<p>        break<\/p>\n<p>    positions.append(start)<\/p>\n<p>    start += len(substring)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5b57\u7b26\u67e5\u627e\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u7ed3\u5408\u5b57\u7b26\u4e32\u5207\u7247\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>pattern = &#39;Hello&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>matches = [m.start() for m in re.finditer(pattern, text)]<\/p>\n<h2><strong>\u7ed3\u5408\u5b57\u7b26\u4e32\u5207\u7247\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406<\/strong><\/h2>\n<p>positions = []<\/p>\n<p>for match in matches:<\/p>\n<p>    positions.append(match)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{pattern}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f18\u5316\u5b57\u7b26\u67e5\u627e\u7684\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5b57\u7b26\u67e5\u627e\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5b57\u5178\u3001\u96c6\u5408\uff09\u6765\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u5e76\u8bb0\u5f55\u6bcf\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>char_positions = {}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u5b57\u7b26\u7684\u51fa\u73b0\u4f4d\u7f6e<\/strong><\/h2>\n<p>for i, char in enumerate(text):<\/p>\n<p>    if char in char_positions:<\/p>\n<p>        char_positions[char].append(i)<\/p>\n<p>    else:<\/p>\n<p>        char_positions[char] = [i]<\/p>\n<h2><strong>\u67e5\u627e\u5b57\u7b26&#39;o&#39;\u7684\u51fa\u73b0\u4f4d\u7f6e<\/strong><\/h2>\n<p>char = &#39;o&#39;<\/p>\n<p>positions = char_positions.get(char, [])<\/p>\n<p>print(f&quot;\u5b57\u7b26&#39;{char}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u5e93\u51fd\u6570\u901a\u5e38\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u6027\u80fd\u4f18\u4e8e\u81ea\u5b9a\u4e49\u5b9e\u73b0\u3002\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u6765\u8fdb\u884c\u5b57\u7b26\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>pattern = &#39;Hello&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>matches = [m.start() for m in re.finditer(pattern, text)]<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{pattern}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{matches}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5206\u800c\u6cbb\u4e4b<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u8f83\u5c0f\u7684\u90e8\u5206\uff0c\u5206\u522b\u8fdb\u884c\u67e5\u627e\uff0c\u7136\u540e\u5408\u5e76\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6bcf\u6b21\u67e5\u627e\u7684\u8303\u56f4\uff0c\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>substring = &quot;Hello&quot;<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u8f83\u5c0f\u7684\u90e8\u5206<\/strong><\/h2>\n<p>parts = [text[i:i+10] for i in range(0, len(text), 10)]<\/p>\n<p>positions = []<\/p>\n<p>for part_index, part in enumerate(parts):<\/p>\n<p>    start = 0<\/p>\n<p>    while True:<\/p>\n<p>        start = part.find(substring, start)<\/p>\n<p>        if start == -1:<\/p>\n<p>            break<\/p>\n<p>        positions.append(part_index * 10 + start)<\/p>\n<p>        start += len(substring)<\/p>\n<p>print(f&quot;\u5b50\u5b57\u7b26\u4e32&#39;{substring}&#39;\u51fa\u73b0\u4f4d\u7f6e\uff1a{positions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u627e\u5230\u7279\u5b9a\u5b57\u7b26\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982find()\u548cindex()\uff09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08\u5982re\u6a21\u5757\uff09\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5b57\u7b26\u67e5\u627e\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u8be6\u5c3d\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u638c\u63e1\u4e86\u5728Python\u4e2d\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u7684\u591a\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u67e5\u627e\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>find()<\/code>\u3001<code>index()<\/code>\u548c<code>in<\/code>\u5173\u952e\u5b57\u3002<code>find()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5b57\u7b26\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de-1\uff1b\u800c<code>index()<\/code>\u65b9\u6cd5\u5728\u5b57\u7b26\u4e0d\u5b58\u5728\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5219\u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u5b57\u7b26\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u67e5\u627e\u5b57\u7b26\u51fa\u73b0\u7684\u6240\u6709\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u627e\u5230\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6240\u6709\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u8bb0\u5f55\u4e0b\u6bcf\u6b21\u51fa\u73b0\u8be5\u5b57\u7b26\u65f6\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u5f97\u5230\u6240\u6709\u4f4d\u7f6e\u7684\u5217\u8868\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;hello world&quot;\nchar = &quot;o&quot;\npositions = [i for i, c in enumerate(text) if c == char]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u67e5\u627e\u65f6\u7684\u5927\u5c0f\u5199\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u67e5\u627e\u5b57\u7b26\u65f6\uff0c\u5927\u5c0f\u5199\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002\u5982\u679c\u5e0c\u671b\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u90fd\u8f6c\u6362\u4e3a\u540c\u4e00\u5927\u5c0f\u5199\u5f62\u5f0f\u3002\u4f7f\u7528<code>lower()<\/code>\u6216<code>upper()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello World&quot;\nchar = &quot;h&quot;\nif char.lower() in text.lower():\n    print(f&quot;&#39;{char}&#39; exists in the string.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u67e5\u627e\u8fc7\u7a0b\u4e0d\u53d7\u5927\u5c0f\u5199\u7684\u9650\u5236\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u627e\u5230\u7279\u5b9a\u5b57\u7b26\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982find()\u548cindex()\uff09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08\u5982 [&hellip;]","protected":false},"author":3,"featured_media":1163889,"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\/1163881"}],"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=1163881"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163881\/revisions"}],"predecessor-version":[{"id":1163892,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163881\/revisions\/1163892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163889"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}