{"id":1096695,"date":"2025-01-08T15:03:59","date_gmt":"2025-01-08T07:03:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096695.html"},"modified":"2025-01-08T15:04:02","modified_gmt":"2025-01-08T07:04:02","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e4%b8%8d%e5%8c%ba%e5%88%86%e5%a4%a7%e5%b0%8f%e5%86%99-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096695.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211812\/a799b756-b6ae-4982-8795-0a249a223456.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff08\u5ffd\u7565\u5927\u5c0f\u5199\uff09\u64cd\u4f5c\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684lower()\u6216upper()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684re.IGNORECASE\u6807\u5fd7\u3001\u5728\u6bd4\u8f83\u5b57\u7b26\u4e32\u65f6\u4f7f\u7528casefold()\u65b9\u6cd5\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u7684lower()\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u548c\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5c06\u6240\u6709\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6548\u679c\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684lower()\u6216upper()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u63d0\u4f9b\u4e86<code>lower()<\/code>\u548c<code>upper()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u3002\u901a\u8fc7\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\uff08\u6216\u5927\u5199\uff09\u540e\u8fdb\u884c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Example using lower() method<\/p>\n<p>str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;hello&quot;<\/p>\n<p>if str1.lower() == str2.lower():<\/p>\n<p>    print(&quot;The strings are equal (ignoring case).&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The strings are not equal.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>str1<\/code>\u548c<code>str2<\/code>\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002<code>upper()<\/code>\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Example using upper() method<\/p>\n<p>str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;HELLO&quot;<\/p>\n<p>if str1.upper() == str2.upper():<\/p>\n<p>    print(&quot;The strings are equal (ignoring case).&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The strings are not equal.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684re.IGNORECASE\u6807\u5fd7<\/h3>\n<\/p>\n<p><p>Python\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93<code>re<\/code>\u63d0\u4f9b\u4e86<code>IGNORECASE<\/code>\u6807\u5fd7\uff0c\u53ef\u4ee5\u5728\u5339\u914d\u8fc7\u7a0b\u4e2d\u5ffd\u7565\u5927\u5c0f\u5199\u3002\u8fd9\u5bf9\u4e8e\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = &quot;hello&quot;<\/p>\n<p>text = &quot;Hello, world!&quot;<\/p>\n<h2><strong>Example using re.IGNORECASE<\/strong><\/h2>\n<p>if re.search(pattern, text, re.IGNORECASE):<\/p>\n<p>    print(&quot;Pattern found (ignoring case).&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Pattern not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.search()<\/code>\u51fd\u6570\u4f7f\u7528\u4e86<code>IGNORECASE<\/code>\u6807\u5fd7\uff0c\u56e0\u6b64\u5b83\u5728\u5339\u914d\u6a21\u5f0f\u65f6\u5ffd\u7565\u4e86\u5927\u5c0f\u5199\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684casefold()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>casefold()<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u66f4\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u8fd8\u5904\u7406\u4e86\u67d0\u4e9b\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4f7f\u5f97\u6bd4\u8f83\u66f4\u52a0\u51c6\u786e\u3002<code>casefold()<\/code>\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0e<code>lower()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Example using casefold() method<\/p>\n<p>str1 = &quot;Stra\u00dfe&quot;<\/p>\n<p>str2 = &quot;strasse&quot;<\/p>\n<p>if str1.casefold() == str2.casefold():<\/p>\n<p>    print(&quot;The strings are equal (ignoring case).&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The strings are not equal.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>casefold()<\/code>\u65b9\u6cd5\u6b63\u786e\u5904\u7406\u4e86\u5fb7\u8bed\u5b57\u7b26\uff0c\u4f7f\u5f97<code>str1<\/code>\u548c<code>str2<\/code>\u5728\u6bd4\u8f83\u65f6\u88ab\u89c6\u4e3a\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u6392\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u952e\u51fd\u6570\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u540e\u518d\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Example using sorted() with a custom key function<\/p>\n<p>words = [&quot;Banana&quot;, &quot;apple&quot;, &quot;Cherry&quot;]<\/p>\n<p>sorted_words = sorted(words, key=lambda s: s.lower())<\/p>\n<p>print(sorted_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>sorted()<\/code>\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u952e\u51fd\u6570<code>lambda s: s.lower()<\/code>\uff0c\u5c06\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u540e\u518d\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u4f7f\u7528<code>lower()<\/code>\u6216<code>upper()<\/code>\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u76f4\u63a5\u548c\u6709\u6548\u7684\u65b9\u5f0f\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684<code>IGNORECASE<\/code>\u6807\u5fd7\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u7279\u5b9a\u8bed\u8a00\u5b57\u7b26\u7684\u573a\u666f\uff0c<code>casefold()<\/code>\u65b9\u6cd5\u5219\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.lower()<\/code>\u6216<code>str.upper()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\uff0c\u7136\u540e\u518d\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5728\u6bd4\u8f83\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u53d7\u5230\u5927\u5c0f\u5199\u7684\u5f71\u54cd\u3002\u4f8b\u5982\uff0c<code>&quot;Hello&quot;.lower() == &quot;hello&quot;.lower()<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<p><strong>Python\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4f55\u5904\u7406\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u6dfb\u52a0<code>re.IGNORECASE<\/code>\u6807\u5fd7\u6765\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c<code>re.search(&#39;pattern&#39;, &#39;Text&#39;, re.IGNORECASE)<\/code>\u5c06\u4f1a\u5339\u914d\u4e0d\u8bba<code>&#39;Text&#39;<\/code>\u7684\u5927\u5c0f\u5199\u5982\u4f55\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u952e\u540d\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1f<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5185\u7f6e\u7684<code>dict<\/code>\u7c7b\uff0c\u91cd\u5199<code>__setitem__<\/code>\u548c<code>__getitem__<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u786e\u4fdd\u5c06\u952e\u8f6c\u6362\u4e3a\u7edf\u4e00\u7684\u5927\u5c0f\u5199\u5f62\u5f0f\uff08\u5982\u5c0f\u5199\uff09\u540e\u518d\u8fdb\u884c\u5b58\u50a8\u548c\u68c0\u7d22\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u5728\u5904\u7406\u952e\u65f6\u5c31\u4f1a\u5ffd\u7565\u5927\u5c0f\u5199\u5dee\u5f02\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff08\u5ffd\u7565\u5927\u5c0f\u5199\uff09\u64cd\u4f5c\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684lower()\u6216u [&hellip;]","protected":false},"author":3,"featured_media":1096705,"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\/1096695"}],"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=1096695"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096695\/revisions"}],"predecessor-version":[{"id":1096706,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096695\/revisions\/1096706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096705"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}