{"id":1139585,"date":"2025-01-08T22:11:45","date_gmt":"2025-01-08T14:11:45","guid":{"rendered":""},"modified":"2025-01-08T22:11:47","modified_gmt":"2025-01-08T14:11:47","slug":"%e5%a6%82%e4%bd%95%e5%8f%96%e6%9c%80%e5%90%8e%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139585.html","title":{"rendered":"\u5982\u4f55\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102838\/ef2ba48c-fd48-4eec-ba6f-3b6fc04a9417.webp\" alt=\"\u5982\u4f55\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26Python\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u7ed3\u5408\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5<\/strong>\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002<strong>\u7d22\u5f15\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u5177\u4f53\u6765\u8bf4\uff0cPython\u7684\u8d1f\u7d22\u5f15\u529f\u80fd\u5141\u8bb8\u4f60\u4ece\u5b57\u7b26\u4e32\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\uff0c\u4ece\u800c\u76f4\u63a5\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d22\u5f15\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u7d22\u5f15\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002\u901a\u8fc7\u8d1f\u7d22\u5f15\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u8d1f\u7d22\u5f15\u4ece-1\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740<code>my_string[-1]<\/code>\u4f1a\u8fd4\u56de<code>my_string<\/code>\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>last_char = my_string[-1]<\/p>\n<p>print(last_char)  # Output: !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u3002\u8d1f\u7d22\u5f15\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5012\u6570\u7b2c\u4e8c\u4e2a\u3001\u5012\u6570\u7b2c\u4e09\u4e2a\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5207\u7247\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5207\u7247\u529f\u80fd\u540c\u6837\u5f3a\u5927\uff0c\u901a\u8fc7\u5207\u7247\u4e5f\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u5207\u7247\u8bed\u6cd5\u662f<code>[start:stop:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>stop<\/code>\u662f\u4e0d\u5305\u62ec\u5728\u5207\u7247\u4e2d\u7684\u7d22\u5f15\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002\u4e3a\u4e86\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[-1:]<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>last_char = my_string[-1:]<\/p>\n<p>print(last_char)  # Output: !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5207\u7247\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u88ab\u6269\u5c55\u5230\u83b7\u53d6\u591a\u4e2a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6700\u540e\u4e24\u4e2a\u5b57\u7b26\u53ef\u4ee5\u4f7f\u7528<code>[-2:]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>str<\/code>\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u4f46\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str<\/code>\u7684<code>len()<\/code>\u51fd\u6570\u7ed3\u5408\u7d22\u5f15\u6765\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>last_char = my_string[len(my_string) - 1]<\/p>\n<p>print(last_char)  # Output: !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u76f4\u63a5\u7d22\u5f15\u6216\u5207\u7247\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u4f1a\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u8003\u8651\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u52a0\u5165\u5176\u4ed6\u903b\u8f91\uff0c\u4f8b\u5982\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_last_char(s):<\/p>\n<p>    if not s:<\/p>\n<p>        return None<\/p>\n<p>    return s[-1]<\/p>\n<p>my_string = &quot;Hello, World!&quot;<\/p>\n<p>last_char = get_last_char(my_string)<\/p>\n<p>print(last_char)  # Output: !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u96c6\u4e2d\u7ba1\u7406\u903b\u8f91\uff0c\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u5730\u65b9\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u590d\u7528\u8fd9\u6bb5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u8fd9\u79cd\u7b80\u5355\u573a\u666f\u4e0b\u53ef\u80fd\u6709\u4e9b\u201c\u5927\u6750\u5c0f\u7528\u201d\uff0c\u4f46\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u64cd\u4f5c\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u786e\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>my_string = &quot;Hello, World!&quot;<\/p>\n<p>match = re.search(r&#39;.$&#39;, my_string)<\/p>\n<p>if match:<\/p>\n<p>    last_char = match.group(0)<\/p>\n<p>    print(last_char)  # Output: !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u5339\u914d\u548c\u66ff\u6362\u529f\u80fd\u3002\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u5b57\u7b26\u4e32\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u5176\u4ed6\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f\u4e0e\u5b9e\u6218<\/h3>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u6269\u5c55\u540d\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d\u662f\u5b57\u7b26\u4e32\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u9700\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6765\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filename = &quot;example.txt&quot;<\/p>\n<p>extension = filename.split(&#39;.&#39;)[-1]<\/p>\n<p>print(extension)  # Output: txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. URL\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406URL\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5224\u65adURL\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4ee5\u786e\u5b9a\u662f\u5426\u662f\u659c\u6760\u7b49\u7b26\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &quot;https:\/\/example.com\/path\/&quot;<\/p>\n<p>if url[-1] == &#39;\/&#39;:<\/p>\n<p>    print(&quot;URL ends with a slash&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;URL does not end with a slash&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u6570\u636e\u7684\u8fc7\u6ee4\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&quot;apple,&quot;, &quot;banana;&quot;, &quot;cherry.&quot;]<\/p>\n<p>cleaned_data = [item.rstrip(&quot;,.;&quot;) for item in data]<\/p>\n<p>print(cleaned_data)  # Output: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u957f\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u80fd\u5bf9\u6027\u80fd\u6709\u5f71\u54cd\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u7d22\u5f15\u548c\u5207\u7247\u65b9\u6cd5\u7684\u6027\u80fd\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u662fO(1)\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_string = &quot;a&quot; * 107 + &quot;z&quot;<\/p>\n<h2><strong>Using indexing<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>last_char = my_string[-1]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Indexing took {end_time - start_time} seconds&quot;)<\/p>\n<h2><strong>Using slicing<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>last_char = my_string[-1:]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Slicing took {end_time - start_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6d4b\u8bd5\u4e2d\uff0c\u7d22\u5f15\u548c\u5207\u7247\u65b9\u6cd5\u7684\u6027\u80fd\u90fd\u975e\u5e38\u9ad8\u6548\uff0c\u51e0\u4e4e\u6ca1\u6709\u660e\u663e\u5dee\u522b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406\u4e0e\u8fb9\u754c\u6761\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u548c\u5176\u4ed6\u8fb9\u754c\u6761\u4ef6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u903b\u8f91\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_last_char(s):<\/p>\n<p>    if not s:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;The string is empty&quot;)<\/p>\n<p>    return s[-1]<\/p>\n<p>try:<\/p>\n<p>    print(get_last_char(&quot;&quot;))<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)  # Output: The string is empty<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u903b\u8f91\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u9762\u5bf9\u5f02\u5e38\u60c5\u51b5\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002<strong>\u7d22\u5f15<\/strong>\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c<strong>\u5207\u7247<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u800c<strong>\u7ed3\u5408\u5185\u7f6e\u51fd\u6570<\/strong>\u548c<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u5219\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u867d\u7136\u5728\u8fd9\u79cd\u7b80\u5355\u573a\u666f\u4e0b\u53ef\u80fd\u4e0d\u592a\u5fc5\u8981\uff0c\u4f46\u5728\u590d\u6742\u5b57\u7b26\u4e32\u5904\u7406\u65f6\u975e\u5e38\u6709\u7528\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u5176\u53ef\u8bfb\u6027\u548c\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d1f\u7d22\u5f15\u6765\u5b89\u5168\u5730\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u8d1f\u7d22\u5f15\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\uff0c\u4f8b\u5982\uff0c<code>string[-1]<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u786e\u4fdd\u5728\u8bbf\u95ee\u4e4b\u524d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u7d22\u5f15\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u5982\u4f55\u5904\u7406\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u4e4b\u524d\uff0c\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528<code>if len(string) &gt; 0:<\/code>\u6761\u4ef6\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u6216\u629b\u51fa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u63d0\u793a\uff0c\u63d0\u9192\u7528\u6237\u8f93\u5165\u6709\u6548\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u9664\u4e86\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u5b57\u7b26\u4e32\u5417\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u4f8b\u5982<code>string[-1:]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>string[len(string)-1]<\/code>\u6765\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u8d1f\u7d22\u5f15\u7b80\u5355\u76f4\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u7ed3\u5408\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u7d22\u5f15\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1139591,"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\/1139585"}],"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=1139585"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139585\/revisions"}],"predecessor-version":[{"id":1139592,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139585\/revisions\/1139592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139591"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}