{"id":70136,"date":"2024-01-29T16:04:56","date_gmt":"2024-01-29T08:04:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=70136"},"modified":"2024-01-29T16:04:56","modified_gmt":"2024-01-29T08:04:56","slug":"python%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2-s-1%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/70136.html","title":{"rendered":"python\u4e2d\u5b57\u7b26\u4e32 s[ : -1]\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p><strong>Python\u4e2d\u7684\u5b57\u7b26\u4e32 s[ : -1] \u8868\u793a\u83b7\u53d6\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u81f3\u5012\u6570\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u7684\u6240\u6709\u5b57\u7b26\uff0c\u6784\u6210\u4e00\u4e2a\u65b0\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u6b64\u64cd\u4f5c\u662f\u5b57\u7b26\u4e32\u5207\u7247\u7684\u4e00\u79cd\u5f62\u5f0f\u3002<\/strong><\/p>\n<p>\u5728\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\u4e2d\uff0cs[ : -1] \u7684\u8bed\u6cd5\u5206\u4e3a\u4e09\u90e8\u5206\uff1a\u8d77\u59cb\u7d22\u5f15\u3001\u7ed3\u675f\u7d22\u5f15\u548c\u6b65\u957f\u3002\u5176\u4e2d\u8d77\u59cb\u7d22\u5f15 `s[` \u7701\u7565\u4e0d\u5199\u4ee3\u8868\u4ece\u5934\u5f00\u59cb\uff0c\u7ed3\u675f\u7d22\u5f15 `-1` \u8868\u793a\u5230\u5b57\u7b26\u4e32\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u524d\u505c\u6b62\uff08\u4e0d\u5305\u62ec\u7d22\u5f15\u4e3a -1 \u7684\u5b57\u7b26\uff09\uff0c\u6b65\u957f\u90e8\u5206\u7701\u7565\u4e0d\u5199\u9ed8\u8ba4\u4e3a 1\uff0c\u610f\u5473\u7740\u4ece\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u9010\u4e2a\u5b57\u7b26\u5411\u540e\u8bfb\u53d6\u3002\u6bd4\u5982\uff0c\u6709\u5b57\u7b26\u4e32 s = &#8220;Hello, World!&#8221;\uff0c\u90a3\u4e48 s[ : -1] \u7684\u7ed3\u679c\u5c06\u662f &#8220;Hello, World&#8221;\u3002\u8fd9\u79cd\u7528\u6cd5\u7ecf\u5e38\u51fa\u73b0\u5728\u9700\u8981\u79fb\u9664\u5b57\u7b26\u4e32\u672b\u5c3e\u5b57\u7b26\u7684\u573a\u666f\u4e2d\uff0c\u5982\u79fb\u9664\u4e00\u4e2a\u53e5\u5b50\u672b\u5c3e\u7684\u6807\u70b9\u7b26\u53f7\u3002<\/p>\n<p><strong>\u4e00\u3001\u7406\u89e3\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/p>\n<p>\u5207\u7247\u662fPython\u4e2d\u5904\u7406\u5e8f\u5217\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u5feb\u901f\u800c\u53c8\u7b80\u6d01\u5730\u8bbf\u95ee\u5e8f\u5217\u7684\u90e8\u5206\u5143\u7d20\u3002\u5b57\u7b26\u4e32s[ : -1] \u53ea\u662f\u5207\u7247\u529f\u80fd\u7684\u4f17\u591a\u7528\u6cd5\u4e4b\u4e00\u3002<\/p>\n<p><strong>\u4e8c\u3001\u5207\u7247\u8bed\u6cd5\u8be6\u89e3<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5207\u7247\u7684\u57fa\u7840\u8bed\u6cd5\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff0c\u5177\u4f53\u4e3a s[start:stop:step]\uff0c\u8fd9\u91cc start \u662f\u5207\u7247\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0cstop \u662f\u5207\u7247\u7ed3\u675f\u4f4d\u7f6e\u7684\u4e0b\u4e00\u4f4d\uff0cstep \u662f\u6b65\u957f\u3002\u82e5\u4e0d\u6307\u660e\u8fd9\u4e9b\u53c2\u6570\uff0c\u9ed8\u8ba4\u4ece\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\uff0c\u76f4\u81f3\u5e8f\u5217\u7ed3\u675f\uff0c\u6b65\u957f\u4e3a 1\u3002\u800c\u8d1f\u6570\u7d22\u5f15\u5219\u4ee3\u8868\u4ece\u5e8f\u5217\u672b\u5c3e\u5f00\u59cb\u8ba1\u7b97\u4f4d\u7f6e\u3002\u5f53 step \u4e3a\u8d1f\u6570\u65f6\uff0c\u5207\u7247\u5c06\u9006\u5e8f\u83b7\u53d6\u5143\u7d20\u3002<\/p>\n<p><strong>\u4e09\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u5b57\u7b26\u4e32\u5207\u7247<\/strong><\/p>\n<p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u548c\u5904\u7406\uff0c\u5982\u53cd\u8f6c\u5b57\u7b26\u4e32\u3001\u5220\u9664\u672b\u5c3e\u7279\u5b9a\u5b57\u7b26\u3001\u63d0\u53d6\u7279\u5b9a\u533a\u95f4\u7684\u5b57\u7b26\u4e32\u7b49\u3002\u7406\u89e3\u5e76\u719f\u7ec3\u638c\u63e1\u5207\u7247\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u4e14\u7b80\u6d01\u6e05\u6670\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u56db\u3001\u4e0e\u5176\u4ed6\u8bed\u8a00\u7684\u5207\u7247\u673a\u5236\u6bd4\u8f83<\/strong><\/p>\n<p>\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u5404\u4e0d\u76f8\u540c\u3002Python\u901a\u8fc7\u7b80\u660e\u7684\u5207\u7247\u8bed\u6cd5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u80fd\u529b\uff0c\u8fd9\u5728\u67d0\u4e9b\u6ca1\u6709\u5207\u7247\u673a\u5236\u6216\u5207\u7247\u4f7f\u7528\u4e0d\u4fbf\u7684\u8bed\u8a00\u4e2d\u5f80\u5f80\u9700\u8981\u7f16\u5199\u989d\u5916\u7684\u51fd\u6570\u6216\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4e94\u3001\u6027\u80fd\u8003\u91cf<\/strong><\/p>\n<p>\u867d\u7136\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\u7b80\u6d01\u65b9\u4fbf\uff0c\u4f46\u5728\u5904\u7406\u6781\u5927\u7684\u5b57\u7b26\u4e32\u65f6\u4ecd\u9700\u6ce8\u610f\u6027\u80fd\u95ee\u9898\u3002\u7531\u4e8e\u5b57\u7b26\u4e32\u5728Python\u4e2d\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u6bcf\u4e00\u6b21\u5207\u7247\u64cd\u4f5c\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6d88\u8017\u8f83\u591a\u5185\u5b58\u548c\u65f6\u95f4\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u5e94\u5f53\u8003\u8651\u5176\u4ed6\u4f18\u5316\u624b\u6bb5\uff0c\u6bd4\u5982\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6216\u5185\u5b58\u89c6\u56fe\u7b49\u3002<\/p>\n<p>\u6b63\u6587\u4f7f\u7528\u201c\u5c0f\u6807\u9898\u201d\u5c06\u6587\u7ae0\u8fdb\u884c\u5206\u6bb5\uff08\u6bcf\u4e2a\u5c0f\u6807\u9898\u4e0b\uff0c\u6700\u5c11\u51992\u4e2a\u6bb5\u843d\u7684\u4ecb\u7ecd\uff09\uff0c\u5c0f\u6807\u9898\u9700\u7528\u5927\u5199\u5b57\u4f53\u6807\u8bb0\uff08\u5217\u5982\uff1a\u4e00\u3001\u4e8c\u3001\u4e09\u3001\u56db\u3001\u7b49\uff0c\uff09\u5e76\u7528\u201c\u3001\u201d\u9694\u5f00<\/p>\n<p> <strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><strong>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32 s[: -1]\u7684\u610f\u601d\u662f\u4ec0\u4e48\u5462\uff1f<\/strong><\/p>\n<p>\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u5b9e\u9645\u4e0a\u662f\u5728\u5bf9\u5b57\u7b26\u4e32 s \u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002s[: -1]\u8868\u793a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\uff0c\u4e00\u76f4\u5207\u7247\u5230\u5012\u6570\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e3a\u6b62\uff08\u4e0d\u5305\u62ec\u5012\u6570\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u4e0d\u5305\u542b\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u8fd9\u79cd\u5207\u7247\u64cd\u4f5c\u6709\u4ec0\u4e48\u5b9e\u9645\u7528\u9014\u5417\uff1f<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u6216\u8005\u8bf4\u662f\u63d0\u53d6\u51fa\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u4e4b\u5916\u7684\u5185\u5bb9\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528s[: -1]\u5c31\u975e\u5e38\u6709\u7528\u4e86\u3002<\/p>\n<p><strong>\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u76f8\u4f3c\u7684\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cPython\u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7684\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528s[2:5]\u6765\u83b7\u53d6\u5b57\u7b26\u4e32s\u4e2d\u7b2c3\u5230\u7b2c5\u4e2a\u5b57\u7b26\u7684\u5b50\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528s[2:]\u83b7\u53d6\u4ece\u7b2c3\u4e2a\u5b57\u7b26\u5230\u672b\u5c3e\u7684\u5b50\u4e32\u3002\u8fd9\u4e9b\u5207\u7247\u64cd\u4f5c\u662fPython\u4e2d\u975e\u5e38\u5e38\u7528\u548c\u5f3a\u5927\u7684\u529f\u80fd\u4e4b\u4e00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u5b57\u7b26\u4e32 s[ : -1] \u8868\u793a\u83b7\u53d6\u4ece\u5b57\u7b26\u4e32\u5f00\u5934\u81f3\u5012\u6570\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u7684\u6240\u6709\u5b57\u7b26\uff0c\u6784\u6210\u4e00\u4e2a\u65b0\u7684\u5b50\u5b57\u7b26\u4e32 [&hellip;]","protected":false},"author":1,"featured_media":70138,"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\/70136"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=70136"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/70136\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/70138"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=70136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=70136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=70136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}