{"id":1094815,"date":"2025-01-08T14:44:47","date_gmt":"2025-01-08T06:44:47","guid":{"rendered":""},"modified":"2025-01-08T14:44:50","modified_gmt":"2025-01-08T06:44:50","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-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094815.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-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210736\/a2e50df0-730c-470f-8693-743657bf71f7.webp\" alt=\"python\u5982\u4f55\u53d6\u6570\u5b57\u540e\u4e09\u4f4d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u4e00\u4e2a\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u53d6\u4f59\u64cd\u4f5c\uff08%\uff09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u540e\u4e09\u4f4d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u53d6\u4f59\u64cd\u4f5c\uff08%\uff09\uff1a<\/strong> \u53d6\u4f59\u64cd\u4f5c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8ba1\u7b97\u65b9\u6cd5\uff0c\u901a\u8fc7\u5bf9\u6570\u5b57\u53d6\u4f59\u6570\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u4f8b\u5982\uff0c<code>number % 1000<\/code> \u5c31\u80fd\u5f97\u5230\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57 <code>123456<\/code>\uff0c<code>123456 % 1000<\/code> \u7684\u7ed3\u679c\u5c31\u662f <code>456<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123456<\/p>\n<p>last_three_digits = number % 1000<\/p>\n<p>print(last_three_digits)  # \u8f93\u51fa 456<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Python\u4e2d\u83b7\u53d6\u6570\u5b57\u540e\u4e09\u4f4d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d6\u4f59\u64cd\u4f5c\uff08%\uff09<\/h3>\n<\/p>\n<p><p>\u53d6\u4f59\u64cd\u4f5c\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u6570\u5b66\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5728Python\u4e2d\uff0c\u901a\u8fc7\u5bf9\u6570\u5b57\u53d6\u4f59\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = number % 1000<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u60c5\u51b5\uff0c\u65e0\u9700\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ec5\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u679c\u662f\u6d6e\u70b9\u6570\uff0c\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u540e\u4e09\u4f4d\u6570\u5b57\uff0c\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = str(number)[-3:]<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/li>\n<li>\u5207\u7247\u64cd\u4f5c\u7075\u6d3b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u4efb\u610f\u4f4d\u7f6e\u7684\u6570\u5b57\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u590d\u6742\u6027\u3002<\/li>\n<li>\u5bf9\u4e8e\u8d1f\u6570\u548c\u5c0f\u6570\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>divmod<\/code>\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5546\u548c\u4f59\u6570\uff0c\u4ece\u800c\u65b9\u4fbf\u5730\u83b7\u53d6\u540e\u4e09\u4f4d\u6570\u5b57\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>_, last_three_digits = divmod(number, 1000)<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\uff0c\u4ee3\u7801\u7b80\u6d01\u9ad8\u6548\u3002<\/li>\n<li>\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5546\u548c\u4f59\u6570\uff0c\u65b9\u4fbf\u8fdb\u884c\u5176\u4ed6\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ec5\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u679c\u662f\u6d6e\u70b9\u6570\uff0c\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u540e\u4e09\u4f4d\u6570\u5b57\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>number = 987654321<\/p>\n<p>match = re.search(r&#39;\\d{3}$&#39;, str(number))<\/p>\n<p>if match:<\/p>\n<p>    last_three_digits = match.group(0)<\/p>\n<p>    print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/li>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u4e00\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u57fa\u7840\u3002<\/li>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = f&quot;{number % 1000:03}&quot;<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li>\u53ef\u4ee5\u65b9\u4fbf\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u590d\u6742\u6027\u3002<\/li>\n<li>\u5bf9\u4e8e\u8d1f\u6570\u548c\u5c0f\u6570\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0c\u53ef\u4ee5\u5c06\u83b7\u53d6\u6570\u5b57\u540e\u4e09\u4f4d\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u4e2d\uff0c\u4fbf\u4e8e\u5728\u591a\u4e2a\u5730\u65b9\u91cd\u590d\u4f7f\u7528\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_last_three_digits(number):<\/p>\n<p>    return str(number)[-3:]<\/p>\n<p>number = 987654321<\/p>\n<p>last_three_digits = get_last_three_digits(number)<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u7528\u6027\u9ad8\uff0c\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<li>\u53ef\u4ee5\u6839\u636e\u9700\u8981\uff0c\u7075\u6d3b\u8c03\u6574\u51fd\u6570\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u989d\u5916\u5b9a\u4e49\u51fd\u6570\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u957f\u5ea6\u3002<\/li>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u7ed3\u5408\u5207\u7247\u548c\u7c7b\u578b\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u7ed3\u5408\u4f7f\u7528\u5207\u7247\u548c\u7c7b\u578b\u8f6c\u6362\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321.123<\/p>\n<p>last_three_digits = int(str(number).split(&#39;.&#39;)[0][-3:])<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/li>\n<li>\u7ed3\u5408\u4f7f\u7528\u5207\u7247\u548c\u7c7b\u578b\u8f6c\u6362\uff0c\u7075\u6d3b\u6027\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u8fdb\u884c\u591a\u6b21\u8f6c\u6362\u548c\u64cd\u4f5c\u3002<\/li>\n<li>\u5bf9\u4e8e\u8d1f\u6570\u548c\u5c0f\u6570\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = &#39;&#39;.join([str(number)[-3:]])<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u590d\u6742\u6027\u3002<\/li>\n<li>\u5bf9\u4e8e\u8d1f\u6570\u548c\u5c0f\u6570\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\uff0c\u4e5f\u53ef\u4ee5\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = number - (number \/\/ 1000) * 1000<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f4\u63a5\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\uff0c\u907f\u514d\u4e86\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5404\u79cd\u60c5\u51b5\uff0c\u65e0\u9700\u989d\u5916\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6570\u5b66\u8fd0\u7b97\u53ef\u80fd\u4e0d\u591f\u76f4\u89c2\uff0c\u4e0d\u6613\u7406\u89e3\u3002<\/li>\n<li>\u4ec5\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u679c\u662f\u6d6e\u70b9\u6570\uff0c\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u83b7\u53d6\u540e\u4e09\u4f4d\uff0c\u518d\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u5904\u7406\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 987654321<\/p>\n<p>last_three_digits = int(str(number)[-3:])<\/p>\n<p>formatted_last_three_digits = f&quot;{last_three_digits:03}&quot;<\/p>\n<p>print(f&quot;\u540e\u4e09\u4f4d\u6570\u5b57\u662f\uff1a{formatted_last_three_digits}&quot;)  # \u8f93\u51fa 321<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u7075\u6d3b\u6027\u66f4\u9ad8\u3002<\/li>\n<li>\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u8c03\u6574\u5b9e\u73b0\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u8fdb\u884c\u591a\u6b21\u8f6c\u6362\u548c\u64cd\u4f5c\u3002<\/li>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u53ef\u80fd\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9009\u62e9\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<strong>\u53d6\u4f59\u64cd\u4f5c\uff08%\uff09\u3001\u5b57\u7b26\u4e32\u5207\u7247\u3001\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7ed3\u5408\u5207\u7247\u548c\u7c7b\u578b\u8f6c\u6362\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\uff0c\u7075\u6d3b\u5730\u83b7\u53d6\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u89e3\u51b3\u4f60\u7684\u95ee\u9898\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\u7684\u65b9\u6cd5\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\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u9009\u62e9\u6700\u540e\u4e09\u4f4d\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6570\u5b57<code>123456<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>str(123456)[-3:]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>&#39;456&#39;<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u63d0\u53d6\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u540e\u4e09\u4f4d\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u63d0\u53d6\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u540e\u4e09\u4f4d\uff0c\u53ef\u4ee5\u5148\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u83b7\u53d6\u5c0f\u6570\u70b9\u540e\u9762\u7684\u6570\u5b57\u3002\u6bd4\u5982\uff0c\u5bf9\u4e8e\u6d6e\u70b9\u6570<code>123.45678<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str(123.45678).split(&#39;.&#39;)<\/code>\u5c06\u5176\u5206\u4e3a\u6574\u6570\u90e8\u5206\u548c\u5c0f\u6570\u90e8\u5206\uff0c\u518d\u53d6\u5c0f\u6570\u90e8\u5206\u7684\u524d\u4e09\u4f4d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8d1f\u6570\u4ee5\u63d0\u53d6\u5176\u540e\u4e09\u4f4d\u6570\u5b57\uff1f<\/strong><br \/>\u63d0\u53d6\u8d1f\u6570\u7684\u540e\u4e09\u4f4d\u4e0e\u6b63\u6570\u7c7b\u4f3c\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7b26\u53f7\u3002\u5728\u5c06\u8d1f\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\uff0c\u53ef\u4ee5\u5ffd\u7565\u8d1f\u53f7\uff0c\u76f4\u63a5\u8fdb\u884c\u5207\u7247\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e<code>-987654<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str(-987654)[-3:]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>&#39;654&#39;<\/code>\uff0c\u4f46\u5982\u679c\u9700\u8981\u4fdd\u7559\u8d1f\u53f7\uff0c\u53ef\u4ee5\u5728\u63d0\u53d6\u540e\u624b\u52a8\u6dfb\u52a0\u8d1f\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u83b7\u53d6\u4e00\u4e2a\u6570\u5b57\u7684\u540e\u4e09\u4f4d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u53d6\u4f59\u64cd\u4f5c\uff08%\uff09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1094827,"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\/1094815"}],"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=1094815"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094815\/revisions"}],"predecessor-version":[{"id":1094828,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094815\/revisions\/1094828"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094827"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}