{"id":1033672,"date":"2024-12-31T11:43:51","date_gmt":"2024-12-31T03:43:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1033672.html"},"modified":"2024-12-31T11:43:53","modified_gmt":"2024-12-31T03:43:53","slug":"python%e5%a6%82%e4%bd%95%e7%94%b1%e5%a4%a7%e5%86%99%e8%bd%ac%e6%8d%a2%e6%88%90%e5%b0%8f%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1033672.html","title":{"rendered":"python\u5982\u4f55\u7531\u5927\u5199\u8f6c\u6362\u6210\u5c0f\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/dc9d1cf4-c646-4115-97f8-15bf35c744db.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7531\u5927\u5199\u8f6c\u6362\u6210\u5c0f\u5199\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c06\u5927\u5199\u8f6c\u6362\u6210\u5c0f\u5199\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>lower()<\/code>\u65b9\u6cd5\u3001<code>casefold()<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca\u901a\u8fc7<code>str.translate()<\/code>\u65b9\u6cd5\u3002\u4eca\u5929\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7528\u6cd5\u3001\u4f18\u7f3a\u70b9\u53ca\u5176\u9002\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u5e76\u4e14\u4fdd\u7559\u5176\u4ed6\u5b57\u7b26\u4e0d\u53d8\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>LOWER()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5<code>lower()<\/code>\u662f\u6700\u5e38\u89c1\u7528\u4e8e\u5c06\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u7684\u65b9\u6cd5\u3002\u5176\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HELLO, WORLD!&quot;<\/p>\n<p>lower_text = text.lower()<\/p>\n<p>print(lower_text)  # \u8f93\u51fa\uff1ahello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>lower()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5bf9\u975e\u5b57\u6bcd\u5b57\u7b26\u4ea7\u751f\u5f71\u54cd\u3002\u56e0\u6b64\uff0c\u5b83\u662f\u975e\u5e38\u65b9\u4fbf\u548c\u9ad8\u6548\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<code>CASEFOLD()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\u662f<code>casefold()<\/code>\uff0c\u5b83\u7c7b\u4f3c\u4e8e<code>lower()<\/code>\uff0c\u4f46\u5728\u5904\u7406\u67d0\u4e9b\u7279\u5b9a\u5b57\u7b26\u96c6\u65f6\uff0c<code>casefold()<\/code>\u66f4\u4e3a\u5f3a\u5927\u3002\u7279\u522b\u662f\u5728\u5904\u7406\u5fb7\u8bed\u7b49\u8bed\u8a00\u65f6\uff0c\u5b83\u80fd\u591f\u5c06\u66f4\u591a\u7684\u7279\u6b8a\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Stra\u00dfe&quot;<\/p>\n<p>casefold_text = text.casefold()<\/p>\n<p>print(casefold_text)  # \u8f93\u51fa\uff1astrasse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c<code>lower()<\/code>\u548c<code>casefold()<\/code>\u7684\u6548\u679c\u662f\u76f8\u540c\u7684\uff0c\u4f46\u5728\u56fd\u9645\u5316\u5e94\u7528\u4e2d\uff0c<code>casefold()<\/code>\u53ef\u80fd\u4f1a\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>str.translate()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>str.translate()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u8f6c\u6362\u5b57\u7b26\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u8f6c\u6362\u8868\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b57\u7b26\u7684\u8f6c\u6362\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>text = &quot;HELLO, WORLD!&quot;<\/p>\n<p>translation_table = str.maketrans(string.ascii_uppercase, string.ascii_lowercase)<\/p>\n<p>translated_text = text.translate(translation_table)<\/p>\n<p>print(translated_text)  # \u8f93\u51fa\uff1ahello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7a0d\u663e\u590d\u6742\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u67d0\u4e9b\u7279\u5b9a\u5b57\u7b26\u8fdb\u884c\u5b9a\u5236\u5316\u8f6c\u6362\u65f6\uff0c\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5982\u4e0a\u8ff0\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u624b\u52a8\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u8f6c\u6362\u5b57\u7b26\u4e5f\u662f\u4e00\u79cd\u53ef\u884c\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u66f4\u52a0\u7075\u6d3b\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HELLO, WORLD!&quot;<\/p>\n<p>lower_text = &quot;&quot;<\/p>\n<p>for char in text:<\/p>\n<p>    if &#39;A&#39; &lt;= char &lt;= &#39;Z&#39;:<\/p>\n<p>        lower_text += chr(ord(char) + 32)<\/p>\n<p>    else:<\/p>\n<p>        lower_text += char<\/p>\n<p>print(lower_text)  # \u8f93\u51fa\uff1ahello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982<code>lower()<\/code>\u548c<code>casefold()<\/code>\u9ad8\u6548\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u8fdb\u884c\u590d\u6742\u5904\u7406\u65f6\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cdPythonic\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u7b80\u6d01\u5730\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HELLO, WORLD!&quot;<\/p>\n<p>lower_text = &#39;&#39;.join([char.lower() if &#39;A&#39; &lt;= char &lt;= &#39;Z&#39; else char for char in text])<\/p>\n<p>print(lower_text)  # \u8f93\u51fa\uff1ahello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u5408\u5728\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7b80\u5355\u8f6c\u6362\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u573a\u666f\u53ca\u6027\u80fd\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5927\u5199\u8f6c\u6362\u4e3a\u5c0f\u5199\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u8981\u6c42\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c<code>lower()<\/code>\u548c<code>casefold()<\/code>\u65b9\u6cd5\u5df2\u7ecf\u8db3\u591f\u4f7f\u7528\uff1b\u5bf9\u4e8e\u9700\u8981\u5bf9\u7279\u5b9a\u5b57\u7b26\u8fdb\u884c\u5b9a\u5236\u5316\u5904\u7406\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>str.translate()<\/code>\u6216\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4ece\u6027\u80fd\u89d2\u5ea6\u6765\u770b\uff0c<code>lower()<\/code>\u548c<code>casefold()<\/code>\u65b9\u6cd5\u6700\u4e3a\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u662fPython\u5185\u7f6e\u65b9\u6cd5\uff0c\u7ecf\u8fc7\u4e86\u9ad8\u5ea6\u4f18\u5316\u3002\u4f7f\u7528<code>str.translate()<\/code>\u65b9\u6cd5\u4e5f\u76f8\u5bf9\u9ad8\u6548\uff0c\u4f46\u5176\u7075\u6d3b\u6027\u4f7f\u5f97\u5176\u5728\u590d\u6742\u8f6c\u6362\u65f6\u8868\u73b0\u51fa\u8272\u3002\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u548c\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u867d\u7136\u63d0\u4f9b\u4e86\u66f4\u591a\u63a7\u5236\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u7565\u900a\u4e00\u7b79\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u5bf9\u6027\u80fd\u7684\u8981\u6c42\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>lower()<\/code>\u548c<code>casefold()<\/code>\u65b9\u6cd5\u5df2\u7ecf\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\uff0c\u662f\u5c06\u5927\u5199\u8f6c\u6362\u4e3a\u5c0f\u5199\u7684\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5<code>lower()<\/code>\u6765\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>&quot;HELLO WORLD&quot;.lower()<\/code>\u5c06\u8fd4\u56de<code>&quot;hello world&quot;<\/code>\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5927\u5199\u8f6c\u5c0f\u5199\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.translate()<\/code>\u65b9\u6cd5\u7ed3\u5408<code>str.maketrans()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5b57\u7b26\u8f6c\u6362\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u5b8c\u6210\u7c7b\u4f3c\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5904\u7406\u7279\u5b9a\u6a21\u5f0f\u65f6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8f6c\u6362\u5927\u5c0f\u5199\u65f6\uff0c\u662f\u5426\u4f1a\u5f71\u54cd\u975e\u5b57\u6bcd\u5b57\u7b26\uff1f<\/strong><br \/>Python\u5728\u6267\u884c\u5927\u5c0f\u5199\u8f6c\u6362\u65f6\uff0c\u4e0d\u4f1a\u5bf9\u975e\u5b57\u6bcd\u5b57\u7b26\u4ea7\u751f\u5f71\u54cd\u3002\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ea\u6709\u5927\u5199\u5b57\u6bcd\u4f1a\u88ab\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u5176\u4ed6\u5b57\u7b26\uff08\u5982\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff09\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u4f7f\u5f97\u8be5\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u5b57\u7b26\u7684\u5e72\u6270\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c06\u5927\u5199\u8f6c\u6362\u6210\u5c0f\u5199\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1033679,"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\/1033672"}],"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=1033672"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033672\/revisions"}],"predecessor-version":[{"id":1033680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033672\/revisions\/1033680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1033679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1033672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1033672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1033672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}