{"id":1142910,"date":"2025-01-08T22:44:57","date_gmt":"2025-01-08T14:44:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142910.html"},"modified":"2025-01-08T22:45:03","modified_gmt":"2025-01-08T14:45:03","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e5%90%ab%e6%9c%89%e6%9f%90%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142910.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\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\/24180808\/3bb357a9-0e31-444a-b01e-a52dc85808d9.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3001<code>str.find()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>str.index()<\/code>\u65b9\u6cd5\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u7b80\u6d01\u7684\u662f<code>in<\/code>\u8fd0\u7b97\u7b26\uff0c\u5b83\u76f4\u63a5\u8fd4\u56de\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u4e3b\u5b57\u7b26\u4e32\u4e2d\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u6700\u76f4\u63a5\u548c\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u4e3b\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_string = &quot;Hello, world!&quot;<\/p>\n<p>sub_string = &quot;world&quot;<\/p>\n<p>if sub_string in main_string:<\/p>\n<p>    print(&quot;Sub-string found!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Sub-string not found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u63cf\u8ff0<\/strong>\uff1a<code>in<\/code>\u8fd0\u7b97\u7b26\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u76f4\u89c2\u6027\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u5f88\u9ad8\u3002\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u65e0\u9700\u5bfc\u5165\u989d\u5916\u7684\u5e93\uff0c\u4e14\u6027\u80fd\u4e5f\u76f8\u5bf9\u8f83\u9ad8\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5305\u542b\u68c0\u67e5\u6765\u8bf4\uff0c\u8fd9\u662f\u6700\u4f18\u9009\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>str.find()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.find()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u4e3b\u5b57\u7b26\u4e32\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u5982\u679c\u627e\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de\u5176\u8d77\u59cb\u7d22\u5f15\uff1b\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_string = &quot;Hello, world!&quot;<\/p>\n<p>sub_string = &quot;world&quot;<\/p>\n<p>index = main_string.find(sub_string)<\/p>\n<p>if index != -1:<\/p>\n<p>    print(f&quot;Sub-string found at index {index}!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Sub-string not found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u52bf<\/strong>\uff1a<code>str.find()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\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\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u641c\u7d22\u548c\u5339\u914d\u5de5\u5177\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>main_string = &quot;Hello, world!&quot;<\/p>\n<p>sub_string = &quot;world&quot;<\/p>\n<p>if re.search(sub_string, main_string):<\/p>\n<p>    print(&quot;Sub-string found!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Sub-string not found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u52bf<\/strong>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5339\u914d\u9700\u6c42\uff0c\u6bd4\u5982\u5ffd\u7565\u5927\u5c0f\u5199\u3001\u591a\u884c\u5339\u914d\u7b49\u3002\u867d\u7136\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>str.index()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.index()<\/code>\u65b9\u6cd5\u4e0e<code>str.find()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u5728\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\u65f6\u4f1a\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\uff0c\u800c\u4e0d\u662f\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_string = &quot;Hello, world!&quot;<\/p>\n<p>sub_string = &quot;world&quot;<\/p>\n<p>try:<\/p>\n<p>    index = main_string.index(sub_string)<\/p>\n<p>    print(f&quot;Sub-string found at index {index}!&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Sub-string not found!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u52bf<\/strong>\uff1a<code>str.index()<\/code>\u65b9\u6cd5\u5728\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u8fd9\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7528\u4e8e\u66f4\u4e25\u683c\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u4e3a\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u77e5\u9053\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.find()<\/code>\u6216<code>str.index()<\/code>\u65b9\u6cd5\u3002\u800c\u5bf9\u4e8e\u590d\u6742\u7684\u5339\u914d\u9700\u6c42\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u5185\u5bb9\u603b\u7ed3<\/strong>\uff1a<code>in<\/code>\u8fd0\u7b97\u7b26\u3001<code>str.find()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>str.index()<\/code>\u65b9\u6cd5\u3002\u8fd9\u56db\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u5b57\u7b26\u4e32\u5339\u914d\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>if &#39;\u5b50\u5b57\u7b26\u4e32&#39; in &#39;\u4e3b\u5b57\u7b26\u4e32&#39;:<\/code>\u53ef\u4ee5\u7b80\u5355\u5730\u8fd4\u56deTrue\u6216False\uff0c\u6307\u793a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u4e3b\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u5404\u79cd\u5b57\u7b26\u4e32\u5339\u914d\u9700\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5b57\u7b26\u4e32\u5339\u914d\u7684\u6548\u7387\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u3002\u901a\u8fc7<code>re.search()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5339\u914d\u64cd\u4f5c\uff0c\u5982\u6a21\u5f0f\u5339\u914d\u3001\u533a\u5206\u5927\u5c0f\u5199\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u66f4\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u641c\u7d22\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u5f71\u54cd\uff1f<\/strong><br \/>\u8981\u5728\u5224\u65ad\u5b57\u7b26\u4e32\u65f6\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u540c\u4e00\u5927\u5c0f\u5199\u5f62\u5f0f\uff0c\u6bd4\u5982\u4f7f\u7528<code>.lower()<\/code>\u6216<code>.upper()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<code>if &#39;\u5b50\u5b57\u7b26\u4e32&#39;.lower() in &#39;\u4e3b\u5b57\u7b26\u4e32&#39;.lower():<\/code> \u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bd4\u8f83\u65f6\u4e0d\u53d7\u5927\u5c0f\u5199\u5f71\u54cd\uff0c\u63d0\u9ad8\u5339\u914d\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528in\u8fd0\u7b97\u7b26\u3001str.find()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001str.in [&hellip;]","protected":false},"author":3,"featured_media":1142926,"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\/1142910"}],"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=1142910"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142910\/revisions"}],"predecessor-version":[{"id":1142929,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142910\/revisions\/1142929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142926"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}