{"id":1058901,"date":"2024-12-31T15:20:29","date_gmt":"2024-12-31T07:20:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058901.html"},"modified":"2024-12-31T15:20:31","modified_gmt":"2024-12-31T07:20:31","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e6%95%b0%e5%ad%97%e5%90%8e%e4%b8%89%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058901.html","title":{"rendered":"python\u5982\u4f55\u53d6\u6570\u5b57\u540e\u4e09\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d3814a26-0c34-4963-9d8a-552d2637bc69.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u53d6\u6570\u5b57\u540e\u4e09\u4f4d\" \/><\/p>\n<p><h3>PYTHON\u5982\u4f55\u53d6\u6570\u5b57\u540e\u4e09\u4f4d<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u53d6\u6a21\u8fd0\u7b97\u3001\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5\u5b9e\u73b0<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u4e00\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u64cd\u4f5c\u6cd5\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u540e\u4e09\u4f4d\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h5>1.1 \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h5>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6700\u5e38\u89c1\u7684\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123456789<\/p>\n<p>str_num = str(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>1.2 \u622a\u53d6\u5b57\u7b26\u4e32<\/h5>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u529f\u80fd\u6765\u622a\u53d6\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">last_three_digits = str_num[-3:]<\/p>\n<p>print(last_three_digits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>last_three_digits<\/code>\u5c06\u4f1a\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684&quot;789&quot;\u3002<\/p>\n<\/p>\n<p><h5>1.3 \u8f6c\u6362\u56de\u6574\u6570<\/h5>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u8f6c\u6362\u56de\u6574\u6570\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">last_three_digits_int = int(last_three_digits)<\/p>\n<p>print(last_three_digits_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u7ec8\uff0c<code>last_three_digits_int<\/code>\u5c06\u4f1a\u662f\u6574\u6570\u7c7b\u578b\u7684789\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u53d6\u6a21\u8fd0\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u53d6\u6a21\u8fd0\u7b97\u662f\u4e00\u79cd\u6570\u5b66\u65b9\u6cd5\uff0c\u901a\u8fc7\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26<code>%<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><h5>2.1 \u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26<\/h5>\n<\/p>\n<p><p>\u53d6\u6a21\u8fd0\u7b97\u7b26<code>%<\/code>\u53ef\u4ee5\u7528\u6765\u83b7\u5f97\u4e00\u4e2a\u6570\u5b57\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c\u53d6\u4e00\u4e2a\u6570\u5b57\u9664\u4ee51000\u7684\u4f59\u6570\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123456789<\/p>\n<p>last_three_digits = num % 1000<\/p>\n<p>print(last_three_digits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u975e\u5e38\u9ad8\u6548\uff0c\u76f4\u63a5\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u5373\u53ef\u5f97\u5230\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u4e09\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6cd5<\/h4>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6cd5\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5229\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u53d6\u540e\u4e09\u4f4d\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h5>3.1 \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h5>\n<\/p>\n<p><p>Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7<code>str.format()<\/code>\u65b9\u6cd5\u6216f-string\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123456789<\/p>\n<p>formatted_num = &#39;{:03d}&#39;.format(num % 1000)<\/p>\n<p>print(formatted_num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;{:03d}&#39;<\/code>\u8868\u793a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u81f3\u5c113\u4f4d\u6570\uff0c\u4e0d\u8db33\u4f4d\u6570\u65f6\u524d\u9762\u8865\u96f6\u3002<\/p>\n<\/p>\n<p><h5>3.2 \u4f7f\u7528f-string<\/h5>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123456789<\/p>\n<p>formatted_num = f&#39;{num % 1000:03d}&#39;<\/p>\n<p>print(formatted_num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u83b7\u5f97\u540e\u4e09\u4f4d\uff0c\u5e76\u4e14\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><h4>\u56db\u3001\u5e94\u7528\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h5>4.1 \u5904\u7406\u7528\u6237\u8f93\u5165<\/h5>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u7535\u8bdd\u53f7\u7801\uff0c\u5e76\u63d0\u53d6\u7535\u8bdd\u53f7\u7801\u7684\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">phone_number = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u7535\u8bdd\u53f7\u7801\uff1a&quot;)<\/p>\n<p>last_three_digits = phone_number[-3:]<\/p>\n<p>print(f&quot;\u7535\u8bdd\u53f7\u7801\u7684\u540e\u4e09\u4f4d\u662f\uff1a{last_three_digits}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>4.2 \u63d0\u53d6\u65e5\u671f\u7684\u540e\u4e09\u4f4d<\/h5>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u63d0\u53d6\u67d0\u4e2a\u65e5\u671f\u7684\u540e\u4e09\u4f4d\uff0c\u4f8b\u5982&quot;20231015&quot;\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;20231015&quot;<\/p>\n<p>last_three_digits = date_str[-3:]<\/p>\n<p>print(f&quot;\u65e5\u671f\u7684\u540e\u4e09\u4f4d\u662f\uff1a{last_three_digits}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>4.3 \u5904\u7406\u5927\u6570\u636e<\/h5>\n<\/p>\n<p><p>\u5728\u5927\u6570\u636e\u5904\u7406\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6279\u91cf\u63d0\u53d6\u6570\u636e\u7684\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [123456, 789012, 345678, 901234]<\/p>\n<p>last_three_digits_list = [str(data)[-3:] for data in data_list]<\/p>\n<p>print(last_three_digits_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6279\u91cf\u5904\u7406\u6570\u636e\u5e76\u63d0\u53d6\u540e\u4e09\u4f4d\u3002<\/p>\n<\/p>\n<p><h4>\u4e94\u3001\u603b\u7ed3<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec<strong>\u5b57\u7b26\u4e32\u64cd\u4f5c\u6cd5\u3001\u53d6\u6a21\u8fd0\u7b97\u6cd5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6cd5<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u6765\u63d0\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u9996\u5148\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u83b7\u53d6\u6700\u540e\u4e09\u4f4d\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">number = 123456\nlast_three_digits = str(number)[-3:]\nprint(last_three_digits)  # \u8f93\u51fa: 456\n<\/code><\/pre>\n<p><strong>\u80fd\u5426\u5904\u7406\u8d1f\u6570\u6216\u5c0f\u6570\u7684\u540e\u4e09\u4f4d\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8d1f\u6570\u548c\u5c0f\u6570\u3002\u5bf9\u4e8e\u8d1f\u6570\uff0c\u8d1f\u53f7\u4f1a\u88ab\u4fdd\u7559\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\uff0c\u800c\u5c0f\u6570\u70b9\u4e5f\u4f1a\u88ab\u89c6\u4e3a\u5b57\u7b26\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\uff0c\u786e\u4fdd\u53ea\u63d0\u53d6\u6570\u5b57\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">negative_number = -12345.678\nlast_three_digits = str(negative_number)[-3:]  # \u8f93\u51fa: .78\n<\/code><\/pre>\n<p>\u5982\u679c\u53ea\u60f3\u63d0\u53d6\u6570\u5b57\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-python\">last_three_digits = str(negative_number).replace(&#39;-&#39;, &#39;&#39;).replace(&#39;.&#39;, &#39;&#39;)[-3:]  # \u8f93\u51fa: 678\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u7528\u5176\u4ed6\u65b9\u6cd5\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff1f<\/strong><br \/>\u9664\u4e86\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u53d6\u6a21\u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5bf9\u4e8e\u6574\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d6\u6a211000\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">number = 123456\nlast_three_digits = number % 1000\nprint(last_three_digits)  # \u8f93\u51fa: 456\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5c0f\u6570\uff0c\u53ef\u4ee5\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u518d\u8fdb\u884c\u64cd\u4f5c\uff0c\u6ce8\u610f\u8981\u5904\u7406\u5c0f\u6570\u70b9\u7684\u4f4d\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON\u5982\u4f55\u53d6\u6570\u5b57\u540e\u4e09\u4f4d \u5728Python\u4e2d\uff0c\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u53d6\u6a21\u8fd0\u7b97\u3001\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1058909,"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\/1058901"}],"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=1058901"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058901\/revisions"}],"predecessor-version":[{"id":1058915,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058901\/revisions\/1058915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058909"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}