{"id":2506,"date":"2025-05-30T17:55:12","date_gmt":"2025-06-04T10:06:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2506"},"modified":"2025-06-04T17:55:12","modified_gmt":"2025-06-04T10:06:12","slug":"python-string-lower-upper","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-string-lower-upper\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Python \u7684 lower() \u548c upper() \u51fd\u6578\uff1a2025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u61c9\u7528\u6280\u5de7"},"content":{"rendered":"<p><meta name=\"description\" content=\"\u5728 Python \u7a0b\u5f0f\u4e2d\uff0c\u4f7f\u7528 lower() \u548c upper() \u51fd\u6578\uff0c\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u5c0f\u5beb\u6216\u5927\u5beb\u3002\u4e86\u89e3\u9019\u4e9b\u51fd\u6578\u7684\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u63d0\u5347\u4f60\u7684\u7a0b\u5f0f\u958b\u767c\u80fd\u529b\u3002\" \/><\/p>\n<h1>\u6df1\u5165\u5b78\u7fd2 Python \u7684 lower() \u548c upper() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u4e2d\uff0c\u5b57\u4e32\uff08String\uff09\u662f\u4e00\u7a2e\u975e\u5e38\u5e38\u898b\u7684\u8cc7\u6599\u578b\u614b\uff0c\u5b83\u53ef\u4ee5\u5132\u5b58\u4e00\u4e32\u6587\u5b57\u3002\u800c\u5728\u4f7f\u7528\u5b57\u4e32\u6642\uff0c\u6211\u5011\u5e38\u6703\u9700\u8981\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u5927\u5beb\u6216\u5c0f\u5beb\uff0c\u9019\u6642\u5c31\u53ef\u4ee5\u4f7f\u7528 Python \u7684 <strong>lower()<\/strong> \u548c <strong>upper()<\/strong> \u51fd\u6578\u3002\u638c\u63e1\u9019\u4e9b\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4e0d\u50c5\u53ef\u4ee5\u8b93\u4f60\u5728\u7de8\u78bc\u6642\u66f4\u70ba\u9748\u6d3b\uff0c\u9084\u80fd\u63d0\u5347\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f lower() \u548c upper() \u51fd\u6578<\/h2>\n<p><strong>lower()<\/strong> \u51fd\u6578\u662f Python \u5167\u5efa\u7684\u51fd\u6578\uff0c\u7528\u4f86\u5c07\u5b57\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u8f49\u63db\u6210\u5c0f\u5beb\u3002\u800c <strong>upper()<\/strong> \u51fd\u6578\u5247\u662f\u5c07\u5b57\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u8f49\u63db\u6210\u5927\u5beb\u3002\u9019\u5169\u500b\u51fd\u6578\u7684\u8a9e\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u62ec\u865f\u4e2d\u8f38\u5165\u5b57\u4e32\u7684\u8b8a\u6578\u540d\u7a31\u5373\u53ef\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c07\u5b57\u4e32 <code>text<\/code> \u8f49\u63db\u6210\u5927\u5beb\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8220;Hello, World!&#8221;<br \/>\ntext = text.upper()<br \/>\nprint(text)  # \u986f\u793a\uff1aHELLO, WORLD!<br \/>\n&#8220;`<\/p>\n<p>\u540c\u6a23\u5730\uff0c\u5982\u679c\u4f60\u60f3\u5c07\u5b57\u4e32 <code>text<\/code> \u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8220;Hello, World!&#8221;<br \/>\ntext = text.lower()<br \/>\nprint(text)  # \u986f\u793a\uff1ahello, world!<br \/>\n&#8220;`<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 lower() \u548c upper() \u51fd\u6578<\/h2>\n<p>\u9664\u4e86\u4e0a\u8ff0\u7528\u6cd5\uff0c<strong>lower()<\/strong> \u548c <strong>upper()<\/strong> \u51fd\u6578\u9084\u6709\u5176\u4ed6\u61c9\u7528\uff1a<\/p>\n<ul>\n<li>\u8f49\u63db\u4e2d\u6587\u5b57\u4e32\uff1a<br \/>\n  &#8220;`python<br \/>\n  text = &#8220;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#8221;<br \/>\n  print(text.upper())  # \u986f\u793a\uff1a\u4f60\u597d\uff0c\u4e16\u754c\uff01<br \/>\n  print(text.lower())  # \u986f\u793a\uff1a\u4f60\u597d\uff0c\u4e16\u754c\uff01<br \/>\n  &#8220;`<br \/>\n  \u6ce8\u610f\uff1a\u4e2d\u6587\u7121\u6cd5\u771f\u6b63\u8f49\u63db\u5927\u5c0f\u5beb\u3002<\/li>\n<li>\u8f49\u63db\u6578\u5b57\uff1a<br \/>\n  &#8220;`python<br \/>\n  text = &#8220;12345&#8221;<br \/>\n  print(text.upper())  # \u986f\u793a\uff1a12345<br \/>\n  print(text.lower())  # \u986f\u793a\uff1a12345<br \/>\n  &#8220;`<br \/>\n  \u6ce8\u610f\uff1a\u6578\u5b57\u4e0d\u53d7\u5927\u5c0f\u5beb\u5f71\u97ff\u3002<\/li>\n<\/ul>\n<h2>\u4f7f\u7528 lower() \u548c upper() \u6642\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 <strong>lower()<\/strong> \u548c <strong>upper()<\/strong> \u51fd\u6578\u6642\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u4e0d\u662f\u5b57\u4e32\uff0c\u6703\u7522\u751f\u932f\u8aa4\uff1a<br \/>\n  &#8220;`python<br \/>\n  text = 123<br \/>\n  text = text.upper()  # \u767c\u751f\u932f\u8aa4\uff1aAttributeError: &#8216;int&#8217; object has no attribute &#8216;upper&#8217;<br \/>\n  &#8220;`\n  <\/li>\n<li>\u50b3\u5165\u7684\u53c3\u6578\u662f\u7a7a\u5b57\u4e32\u6642\uff0c\u6703\u56de\u50b3\u7a7a\u5b57\u4e32\uff1a<br \/>\n  &#8220;`python<br \/>\n  text = &#8220;&#8221;<br \/>\n  text = text.upper()<br \/>\n  print(text)  # \u986f\u793a\uff1a<br \/>\n  &#8220;`\n  <\/li>\n<li>\u7576\u50b3\u5165\u7684\u53c3\u6578\u70ba None \u6642\uff0c\u6703\u767c\u751f\u932f\u8aa4\uff1a<br \/>\n  &#8220;`python<br \/>\n  text = None<br \/>\n  text = text.upper()  # \u767c\u751f\u932f\u8aa4\uff1aAttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;upper&#8217;<br \/>\n  &#8220;`\n  <\/li>\n<\/ul>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u5b57\u4e32\u5927\u5c0f\u5beb\u8f49\u63db<\/h2>\n<p>\u9019\u88e1\u6709\u4e00\u500b\u5be6\u969b\u7684\u61c9\u7528\u7bc4\u4f8b\u3002\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u540d\u70ba <code>text<\/code> \u7684\u5b57\u4e32\uff0c\u88e1\u9762\u5b58\u6709\u4e00\u6bb5\u6587\u5b57\uff0c\u4f60\u60f3\u8981\u5c07\u6587\u5b57\u5168\u90e8\u8f49\u63db\u6210\u5927\u5beb\uff0c\u63a5\u8457\u518d\u8f49\u63db\u6210\u5c0f\u5beb\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8220;Python is a programming language.&#8221;<br \/>\ntext = text.upper()<br \/>\ntext = text.lower()<br \/>\nprint(text)  # \u986f\u793a\uff1apython is a programming language.<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>lower()<\/strong> \u548c <strong>upper()<\/strong> \u51fd\u6578\u9032\u884c\u5b57\u4e32\u7684\u6bd4\u8f03\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5224\u65b7\u5b57\u4e32 <code>text1<\/code> \u548c <code>text2<\/code> \u662f\u5426\u76f8\u7b49\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext1 = &#8220;Python&#8221;<br \/>\ntext2 = &#8220;python&#8221;<br \/>\nif text1.lower() == text2.lower():<br \/>\n    print(&#8220;text1 \u548c text2 \u76f8\u7b49&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;text1 \u548c text2 \u4e0d\u76f8\u7b49&#8221;)<br \/>\n# \u986f\u793a\uff1atext1 \u548c text2 \u76f8\u7b49<br \/>\n&#8220;`<\/p>\n<p>\u900f\u904e\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\uff0c\u53ef\u4ee5\u8f15\u9b06\u5ffd\u7565\u5927\u5c0f\u5beb\u7684\u5dee\u7570\u9032\u884c\u5b57\u4e32\u6bd4\u8f03\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u8655\u7406\u540d\u7a31\u5217\u8868<\/h2>\n<p>\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u540d\u70ba <code>names<\/code> \u7684\u4e32\u5217\uff0c\u88e1\u9762\u5b58\u6709\u82e5\u5e72\u500b\u4eba\u540d\uff0c\u60f3\u8981\u5c07\u6240\u6709\u4eba\u540d\u8f49\u63db\u6210\u5927\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnames = [&#8216;John&#8217;, &#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Eve&#8217;]\nfor i in range(len(names)):<br \/>\n    names[i] = names[i].upper()<br \/>\nprint(names)  # \u986f\u793a\uff1a[&#8216;JOHN&#8217;, &#8216;ALICE&#8217;, &#8216;BOB&#8217;, &#8216;EVE&#8217;]\n&#8220;`<\/p>\n<p>\u82e5\u60f3\u8981\u5c07\u4eba\u540d\u8f49\u63db\u6210\u5c0f\u5beb\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnames = [&#8216;John&#8217;, &#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Eve&#8217;]\nfor i in range(len(names)):<br \/>\n    names[i] = names[i].lower()<br \/>\nprint(names)  # \u986f\u793a\uff1a[&#8216;john&#8217;, &#8216;alice&#8217;, &#8216;bob&#8217;, &#8216;eve&#8217;]\n&#8220;`<\/p>\n<p>\u9084\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5c0e\u5f0f\u4f86\u5be6\u73fe\uff1a<\/p>\n<p>&#8220;`python<br \/>\nnames = [&#8216;John&#8217;, &#8216;Alice&#8217;, &#8216;Bob&#8217;, &#8216;Eve&#8217;]\nnames_upper = [name.upper() for name in names]\nprint(names_upper)  # \u986f\u793a\uff1a[&#8216;JOHN&#8217;, &#8216;ALICE&#8217;, &#8216;BOB&#8217;, &#8216;EVE&#8217;]\n&#8220;`<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 <strong>lower()<\/strong> \u548c <strong>upper()<\/strong> \u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u65b9\u4fbf\u5730\u9032\u884c\u5b57\u4e32\u5927\u5c0f\u5beb\u8f49\u63db\u3002\u6211\u5011\u4e5f\u8ac7\u5230\u4e86\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u6642\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9805\u4ee5\u53ca\u5be6\u969b\u7684\u61c9\u7528\u7bc4\u4f8b\u3002\u5e0c\u671b\u900f\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u4f60\u80fd\u5728 Python \u7a0b\u5f0f\u4e2d\u66f4\u52a0\u9748\u6d3b\u5730\u904b\u7528\u9019\u4e9b\u51fd\u6578\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5b57\u4e32\u8655\u7406\u6280\u5de7\uff0c\u6b61\u8fce\u53c3\u8003\u6211\u5011\u7684[Python \u5b57\u4e32\u8655\u7406\u76f8\u95dc\u6587\u737b](https:\/\/vocus.cc)\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u6280\u80fd\u518d\u4e0a\u5c64\u6a13\uff01<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<ul>\n<li><strong>Q: lower() \u548c upper() \u51fd\u6578\u662f\u5426\u53ef\u4ee5\u8655\u7406\u6240\u6709\u5b57\u4e32\uff1f<\/strong><br \/>\n  A: \u9019\u5169\u500b\u51fd\u6578\u4e3b\u8981\u7528\u65bc\u5b57\u6bcd\u5b57\u5143\uff0c\u5c0d\u65bc\u6578\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\u4e0d\u6703\u6709\u5f71\u97ff\u3002<\/li>\n<li><strong>Q: \u4f7f\u7528 lower() \u548c upper() \u51fd\u6578\u662f\u5426\u6703\u5f71\u97ff\u539f\u59cb\u5b57\u4e32\uff1f<\/strong><br \/>\n  A: \u662f\u7684\uff0c\u9019\u4e9b\u51fd\u6578\u6703\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5b57\u4e32\uff0c\u539f\u59cb\u5b57\u4e32\u4e0d\u6703\u6539\u8b8a\u3002<\/li>\n<li><strong>Q: \u5982\u4f55\u8655\u7406\u5927\u5c0f\u5beb\u4e0d\u654f\u611f\u7684\u6bd4\u8f03\uff1f<\/strong><br \/>\n  A: \u4f7f\u7528 lower() \u6216 upper() \u51fd\u6578\u5c07\u5169\u500b\u5b57\u4e32\u8f49\u63db\u70ba\u540c\u4e00\u5927\u5c0f\u5beb\u9032\u884c\u6bd4\u8f03\u3002<\/li>\n<\/ul>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5b78\u7fd2 Python \u7684 lower() \u548c u&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[],"class_list":["post-2506","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Eq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2506"}],"version-history":[{"count":7,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2506\/revisions"}],"predecessor-version":[{"id":13067,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2506\/revisions\/13067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}