{"id":1101651,"date":"2025-01-08T15:51:48","date_gmt":"2025-01-08T07:51:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101651.html"},"modified":"2025-01-08T15:51:50","modified_gmt":"2025-01-08T07:51:50","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e5%8c%b9%e9%85%8d%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101651.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u5982\u4f55\u5339\u914d\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\/25064322\/e19f8489-700e-47a1-9016-8cd1ca4a95f6.webp\" alt=\"python\u5b57\u7b26\u4e32\u5982\u4f55\u5339\u914d\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u5b57\u7b26\u4e32\u5339\u914d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001<code>str.find()<\/code>\u65b9\u6cd5\u3001<code>str.index()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u548c<code>str.startswith()<\/code>\u53ca<code>str.endswith()<\/code>\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d<\/strong>\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u6765\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u5b57\u7b26\u4e32\u5339\u914d\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\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, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;welcome&quot;<\/p>\n<p>if substring in main_string:<\/p>\n<p>    print(&quot;Substring found!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Substring not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>in<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u662f\u5426\u627e\u5230\u4e86\u5b50\u5b57\u7b26\u4e32\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\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6700\u4f4e\u7d22\u5f15\uff0c\u5982\u679c\u627e\u4e0d\u5230\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_string = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;welcome&quot;<\/p>\n<p>index = main_string.find(substring)<\/p>\n<p>if index != -1:<\/p>\n<p>    print(f&quot;Substring found at index {index}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Substring not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\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>\u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u627e\u4e0d\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_string = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>substring = &quot;welcome&quot;<\/p>\n<p>try:<\/p>\n<p>    index = main_string.index(substring)<\/p>\n<p>    print(f&quot;Substring found at index {index}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Substring not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u8fdb\u884c\u590d\u6742\u6a21\u5f0f\u5339\u914d\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>main_string = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>pattern = r&quot;welcome&quot;<\/p>\n<p>if re.search(pattern, main_string):<\/p>\n<p>    print(&quot;Pattern found!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Pattern not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>re.search()<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u5219\u8fd4\u56de<code>None<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>str.startswith()<\/code>\u548c<code>str.endswith()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u5f00\u5934\u6216\u7ed3\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_string = &quot;Hello, welcome to the world of Python&quot;<\/p>\n<p>if main_string.startswith(&quot;Hello&quot;):<\/p>\n<p>    print(&quot;String starts with &#39;Hello&#39;&quot;)<\/p>\n<p>if main_string.endswith(&quot;Python&quot;):<\/p>\n<p>    print(&quot;String ends with &#39;Python&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\u6216regexp\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u80fd\u529b\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u67d0\u79cd\u6a21\u5f0f\uff0c\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u7c7b<\/strong>\uff1a\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u7c7b\u3002\u4f8b\u5982\uff0c<code>[a-z]<\/code>\u8868\u793a\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\uff0c<code>[0-9]<\/code>\u8868\u793a\u6240\u6709\u6570\u5b57\u3002<\/li>\n<li><strong>\u9884\u5b9a\u4e49\u5b57\u7b26\u7c7b<\/strong>\uff1a\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u7c7b\u6709\u9884\u5b9a\u4e49\u7684\u7b80\u5199\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c<code>\\d<\/code>\u8868\u793a\u6570\u5b57\uff0c<code>\\w<\/code>\u8868\u793a\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\uff0c<code>\\s<\/code>\u8868\u793a\u7a7a\u767d\u5b57\u7b26\u3002<\/li>\n<li><strong>\u91cf\u8bcd<\/strong>\uff1a\u91cf\u8bcd\u7528\u6765\u6307\u5b9a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c<code>*<\/code>\u8868\u793a\u96f6\u6b21\u6216\u591a\u6b21\uff0c<code>+<\/code>\u8868\u793a\u4e00\u6b21\u6216\u591a\u6b21\uff0c<code>?<\/code>\u8868\u793a\u96f6\u6b21\u6216\u4e00\u6b21\uff0c<code>{m,n}<\/code>\u8868\u793a\u81f3\u5c11m\u6b21\uff0c\u81f3\u591an\u6b21\u3002<\/li>\n<li><strong>\u8fb9\u754c\u5339\u914d<\/strong>\uff1a<code>^<\/code>\u8868\u793a\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\uff0c<code>$<\/code>\u8868\u793a\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u3002<\/li>\n<li><strong>\u5206\u7ec4\u548c\u6355\u83b7<\/strong>\uff1a\u4f7f\u7528\u5706\u62ec\u53f7<code>()<\/code>\u5c06\u90e8\u5206\u6a21\u5f0f\u5206\u7ec4\uff0c\u5e76\u6355\u83b7\u5339\u914d\u7684\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528<code>\\1<\/code>\u3001<code>\\2<\/code>\u7b49\u6765\u5f15\u7528\u6355\u83b7\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><h4>\u4f8b\u5b50\uff1a\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$&quot;<\/p>\n<p>email = &quot;example@example.com&quot;<\/p>\n<p>if re.match(pattern, email):<\/p>\n<p>    print(&quot;Valid email address&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid email address&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f8b\u5b50<\/h3>\n<\/p>\n<p><h4>1\u3001\u63d0\u53d6\u7535\u8bdd\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u6bb5\u6587\u672c\uff0c\u60f3\u8981\u4ece\u4e2d\u63d0\u53d6\u6240\u6709\u7684\u7535\u8bdd\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Contact us at 123-456-7890 or 987-654-3210&quot;<\/p>\n<p>pattern = r&quot;\\d{3}-\\d{3}-\\d{4}&quot;<\/p>\n<p>phone_numbers = re.findall(pattern, text)<\/p>\n<p>print(&quot;Phone numbers found:&quot;, phone_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>\\d{3}-\\d{3}-\\d{4}<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u7535\u8bdd\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u60f3\u8981\u5c06\u6587\u672c\u4e2d\u7684\u6240\u6709\u65e5\u671f\u683c\u5f0f\u4ece<code>MM\/DD\/YYYY<\/code>\u66ff\u6362\u4e3a<code>YYYY-MM-DD<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Today&#39;s date is 12\/31\/2023&quot;<\/p>\n<p>pattern = r&quot;(\\d{2})\/(\\d{2})\/(\\d{4})&quot;<\/p>\n<p>replacement = r&quot;\\3-\\1-\\2&quot;<\/p>\n<p>new_text = re.sub(pattern, replacement, text)<\/p>\n<p>print(&quot;Updated text:&quot;, new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>(\\d{2})\/(\\d{2})\/(\\d{4})<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u65e5\u671f\u683c\u5f0f\uff0c<code>\\3-\\1-\\2<\/code>\u662f\u66ff\u6362\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u5c06\u65e5\u671f\u683c\u5f0f\u8f6c\u6362\u4e3a<code>YYYY-MM-DD<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u9a8c\u8bc1\u5bc6\u7801\u5f3a\u5ea6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u60f3\u8981\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u5bc6\u7801\u662f\u5426\u7b26\u5408\u5f3a\u5ea6\u8981\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>password = &quot;StrongPass1!&quot;<\/p>\n<p>pattern = r&quot;^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&amp;])[A-Za-z\\d@$!%*?&amp;]{8,}$&quot;<\/p>\n<p>if re.match(pattern, password):<\/p>\n<p>    print(&quot;Password is strong&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Password is weak&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&amp;])[A-Za-z\\d@$!%*?&amp;]{8,}$<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5c0f\u5199\u5b57\u6bcd\u3001\u4e00\u4e2a\u5927\u5199\u5b57\u6bcd\u3001\u4e00\u4e2a\u6570\u5b57\u3001\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u4e14\u957f\u5ea6\u81f3\u5c11\u4e3a8\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u5305\u62ec\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001<code>str.find()<\/code>\u65b9\u6cd5\u3001<code>str.index()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u548c<code>str.startswith()<\/code>\u53ca<code>str.endswith()<\/code>\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u5339\u914d\u95ee\u9898\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u6a21\u5f0f\u8bc6\u522b\u4efb\u52a1\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u52a0\u81ea\u5982\u5730\u5904\u7406\u5b57\u7b26\u4e32\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002\u901a\u8fc7<code>re.search()<\/code>\u3001<code>re.match()<\/code>\u548c<code>re.findall()<\/code>\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.search(r&#39;\u6a21\u5f0f&#39;, &#39;\u76ee\u6807\u5b57\u7b26\u4e32&#39;)<\/code>\u53ef\u4ee5\u67e5\u627e\u76ee\u6807\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u6a21\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83\u548c\u5339\u914d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c<code>str.startswith()<\/code>\u548c<code>str.endswith()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u7684\u524d\u7f00\u6216\u540e\u7f00\u5f00\u59cb\u6216\u7ed3\u675f\u3002\u5bf9\u4e8e\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.lower()<\/code>\u6216<code>str.upper()<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u76f8\u540c\u7684\u5927\u5c0f\u5199\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u5b57\u7b26\u4e32\u5339\u914d\u95ee\u9898\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u8fdb\u884c\u5927\u5c0f\u5199\u654f\u611f\u7684\u5339\u914d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982\u4f7f\u7528<code>==<\/code>\u6765\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u5e0c\u671b\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\uff0c\u4f8b\u5982\uff1a<code>str1.lower() == str2.lower()<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5339\u914d\u4e0d\u53d7\u5927\u5c0f\u5199\u7684\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u7b26\u4e32\u5339\u914d\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528in\u5173\u952e\u5b57\u3001str.find()\u65b9\u6cd5\u3001str.index() [&hellip;]","protected":false},"author":3,"featured_media":1101659,"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\/1101651"}],"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=1101651"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101651\/revisions"}],"predecessor-version":[{"id":1101662,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101651\/revisions\/1101662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101659"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}