{"id":2511,"date":"2025-06-02T17:34:47","date_gmt":"2025-06-04T10:05:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2511"},"modified":"2025-06-04T17:34:47","modified_gmt":"2025-06-04T10:05:46","slug":"python-string-strip-lstrip-rstrip","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-string-strip-lstrip-rstrip\/","title":{"rendered":"\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\uff1a\u6df1\u5165\u4e86\u89e3 strip()\u3001lstrip() \u548c rstrip() \u51fd\u6578"},"content":{"rendered":"<h1>\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\uff1a\u6df1\u5165\u4e86\u89e3 strip()\u3001lstrip() \u548c rstrip() \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\u5728\u4f7f\u7528\u5b57\u4e32\u6642\uff0c\u6211\u5011\u7d93\u5e38\u9700\u8981\u5c0d\u5b57\u4e32\u9032\u884c\u53bb\u9664\u7a7a\u683c\u7684\u64cd\u4f5c\uff0c\u9019\u6642\u5c31\u53ef\u4ee5\u4f7f\u7528 Python \u7684 <strong>strip()<\/strong>\u3001<strong>lstrip()<\/strong> \u548c <strong>rstrip()<\/strong> \u51fd\u6578\u3002\u9019\u4e9b\u51fd\u6578\u662f Python \u5167\u5efa\u7684\uff0c\u80fd\u5920\u8f15\u9b06\u8655\u7406\u5b57\u4e32\u7684\u7a7a\u767d\u5b57\u5143\u3002<\/p>\n<p>\u70ba\u4e86\u8b93\u5927\u5bb6\u66f4\u6df1\u5165\u4e86\u89e3 <strong>strip()<\/strong>\u3001<strong>lstrip()<\/strong> \u548c <strong>rstrip()<\/strong> \u51fd\u6578\u7684\u7528\u6cd5\uff0c\u6211\u5011\u5c07\u6db5\u84cb\u4ee5\u4e0b\u4e3b\u984c\uff1a<\/p>\n<p>&#8211; \u4ec0\u9ebc\u662f String \u7684 strip()\u3001lstrip() \u548c rstrip() \u51fd\u6578<br \/>\n&#8211; \u5982\u4f55\u4f7f\u7528 strip()\u3001lstrip() \u548c rstrip() \u51fd\u6578<br \/>\n&#8211; \u6ce8\u610f\u4e8b\u9805\u8207\u932f\u8aa4\u6392\u9664<br \/>\n&#8211; \u5be6\u4f5c\u7bc4\u4f8b\u8207\u5ef6\u4f38\u61c9\u7528<\/p>\n<h2>\u4ec0\u9ebc\u662f String \u7684 strip()\u3001lstrip() \u548c rstrip() \u51fd\u6578<\/h2>\n<p><strong>strip()<\/strong> \u51fd\u6578\u662f Python \u5167\u5efa\u7684\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u53bb\u9664\u5b57\u4e32\u524d\u5f8c\u7684\u7a7a\u767d\u5b57\u5143\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u63db\u884c\u7b26\u7b49\u3002\u6b64\u51fd\u6578\u5c0d\u65bc\u6e05\u7406\u7528\u6236\u8f38\u5165\u6216\u8655\u7406\u6587\u672c\u6578\u64da\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u9664\u4e86 <strong>strip()<\/strong> \u51fd\u6578\u4e4b\u5916\uff0cPython \u9084\u6709\u5176\u4ed6\u5169\u500b\u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u53bb\u9664\u5b57\u4e32\u7684\u7a7a\u767d\u5b57\u5143\uff0c\u5206\u5225\u662f\uff1a<\/p>\n<p>&#8211; <strong>lstrip()<\/strong>\uff1a\u7528\u4f86\u53bb\u9664\u5b57\u4e32\u5de6\u908a\u7684\u7a7a\u767d\u5b57\u5143\u3002<br \/>\n&#8211; <strong>rstrip()<\/strong>\uff1a\u7528\u4f86\u53bb\u9664\u5b57\u4e32\u53f3\u908a\u7684\u7a7a\u767d\u5b57\u5143\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 strip()\u3001lstrip() \u548c rstrip() \u51fd\u6578<\/h2>\n<p>\u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u62ec\u865f\u4e2d\u8f38\u5165\u5b57\u4e32\u7684\u8b8a\u6578\u540d\u7a31\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7bc4\u4f8b\uff1a<\/p>\n<p>\u53bb\u9664\u5b57\u4e32\u524d\u5f8c\u7684\u7a7a\u767d\u5b57\u5143\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"   Hello, World!   \"\ntext = text.strip()\nprint(text)  # \u986f\u793a\uff1aHello, World!\n<\/pre>\n<p>\u53bb\u9664\u5b57\u4e32\u5de6\u908a\u7684\u7a7a\u767d\u5b57\u5143\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"   Hello, World!   \"\ntext = text.lstrip()\nprint(text)  # \u986f\u793a\uff1aHello, World!   \n<\/pre>\n<p>\u53bb\u9664\u5b57\u4e32\u53f3\u908a\u7684\u7a7a\u767d\u5b57\u5143\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"   Hello, World!   \"\ntext = text.rstrip()\nprint(text)  # \u986f\u793a\uff1a   Hello, World!\n<\/pre>\n<h2>\u6ce8\u610f\u4e8b\u9805\u8207\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 <strong>strip()<\/strong>\u3001<strong>lstrip()<\/strong> \u548c <strong>rstrip()<\/strong> \u51fd\u6578\u6642\uff0c\u8acb\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>1. **\u50b3\u5165\u975e\u5b57\u4e32\u53c3\u6578\u6703\u767c\u751f\u932f\u8aa4**\uff1a<\/p>\n<pre class=\"brush: python\">\n   text = 123\n   text = text.strip()  # \u767c\u751f\u932f\u8aa4\uff1aAttributeError: 'int' object has no attribute 'strip'\n   <\/pre>\n<p>2. **\u50b3\u5165\u7a7a\u5b57\u4e32\u6703\u8fd4\u56de\u7a7a\u5b57\u4e32**\uff1a<\/p>\n<pre class=\"brush: python\">\n   text = \"\"\n   text = text.strip()\n   print(text)  # \u986f\u793a\uff1a\n   <\/pre>\n<p>3. **\u50b3\u5165 None \u6703\u767c\u751f\u932f\u8aa4**\uff1a<\/p>\n<pre class=\"brush: python\">\n   text = None\n   text = text.strip()  # \u767c\u751f\u932f\u8aa4\uff1aAttributeError: 'NoneType' object has no attribute 'strip'\n   <\/pre>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u4f86\u770b\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\u5305\u542b\u82e5\u5e72\u500b\u7528\u6236\u540d\uff0c\u4e26\u4e14\u6bcf\u500b\u7528\u6236\u540d\u524d\u5f8c\u90fd\u6709\u7a7a\u767d\u5b57\u5143\u3002\u4f60\u60f3\u8981\u522a\u9664\u6bcf\u500b\u7528\u6236\u540d\u524d\u5f8c\u7684\u7a7a\u767d\u5b57\u5143\uff0c\u4e26\u5c07\u7528\u6236\u540d\u5132\u5b58\u5728\u4e00\u500b\u4e32\u5217\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"  Alice   Bob   Charlie   \"\nnames = text.split()  # \u5c07\u5b57\u4e32\u5206\u5272\u6210\u4e32\u5217\nnames = [name.strip() for name in names]  # \u522a\u9664\u6bcf\u500b\u7528\u6236\u540d\u524d\u5f8c\u7684\u7a7a\u767d\u5b57\u5143\nprint(names)  # \u986f\u793a\uff1a['Alice', 'Bob', 'Charlie']\n<\/pre>\n<p>\u4ee5\u4e0a\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6709\u6548\u5229\u7528 <strong>strip()<\/strong>\u3001<strong>lstrip()<\/strong> \u548c <strong>rstrip()<\/strong> \u51fd\u6578\u4f86\u8655\u7406\u5b57\u4e32\uff0c\u9019\u5728\u6578\u64da\u6e05\u7406\u548c\u7528\u6236\u8f38\u5165\u9a57\u8b49\u4e2d\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>\u82e5\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u5b57\u4e32\u8655\u7406\u6280\u5de7\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6df1\u5165\u6559\u5b78](https:\/\/vocus.cc\/article\/5f3e8e01fd89780001c1f30c)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. strip()\u3001lstrip() \u548c rstrip() \u4e4b\u9593\u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p><strong>strip()<\/strong> \u7528\u65bc\u53bb\u9664\u5b57\u4e32\u524d\u5f8c\u7684\u7a7a\u767d\u5b57\u5143\uff0c\u800c <strong>lstrip()<\/strong> \u548c <strong>rstrip()<\/strong> \u5206\u5225\u7528\u65bc\u53bb\u9664\u5de6\u908a\u548c\u53f3\u908a\u7684\u7a7a\u767d\u5b57\u5143\u3002<\/p>\n<h3>2. \u5982\u679c\u6211\u60f3\u8981\u53bb\u9664\u7279\u5b9a\u5b57\u7b26\uff0c\u8a72\u600e\u9ebc\u505a\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728\u9019\u4e9b\u51fd\u6578\u4e2d\u50b3\u5165\u8981\u53bb\u9664\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<code>text.strip('abc')<\/code> \u5c07\u53bb\u9664\u5b57\u4e32\u524d\u5f8c\u7684 &#8216;a&#8217;\u3001&#8217;b&#8217; \u548c &#8216;c&#8217; \u5b57\u7b26\u3002<\/p>\n<h3>3. \u4f7f\u7528\u9019\u4e9b\u51fd\u6578\u6703\u5f71\u97ff\u539f\u59cb\u5b57\u4e32\u55ce\uff1f<\/h3>\n<p>\u4e0d\u6703\uff0c\u9019\u4e9b\u51fd\u6578\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5b57\u4e32\uff0c\u539f\u59cb\u5b57\u4e32\u4fdd\u6301\u4e0d\u8b8a\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u4f60\u7684 Python \u6280\u80fd\uff1a\u6df1\u5165\u4e86\u89e3 strip&#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-2511","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-Ev","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2511","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=2511"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2511\/revisions"}],"predecessor-version":[{"id":13066,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2511\/revisions\/13066"}],"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=2511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}