{"id":971042,"date":"2024-12-27T05:31:23","date_gmt":"2024-12-26T21:31:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971042.html"},"modified":"2024-12-27T05:31:25","modified_gmt":"2024-12-26T21:31:25","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e4%b8%baint%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971042.html","title":{"rendered":"python\u5982\u4f55\u6539\u4e3aint\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194056\/42b0914b-fba5-4fcb-847f-85754cf0304c.webp\" alt=\"python\u5982\u4f55\u6539\u4e3aint\u7c7b\u578b\" \/><\/p>\n<p><p> \u5c06Python\u4e2d\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u3002<strong>\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3001\u786e\u4fdd\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u8f6c\u6362\u3001\u5904\u7406\u6d6e\u70b9\u6570\u7684\u622a\u65ad<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728Python\u4e2d\uff0c<code>int()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u662f\u5904\u7406\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u57fa\u7840\u65b9\u6cd5\u4e4b\u4e00\u3002<code>int()<\/code>\u51fd\u6570\u80fd\u591f\u5904\u7406\u5b57\u7b26\u4e32\u3001\u6d6e\u70b9\u6570\u4ee5\u53ca\u5176\u4ed6\u6570\u5b57\u7c7b\u578b\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u8f93\u5165\u7684\u683c\u5f0f\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u8868\u793a\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u6709\u6548\u7684\u6570\u5b57\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>int()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>int()<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u53c2\u6570\u7684\u6574\u6570\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\u88ab\u6210\u529f\u8f6c\u6362\u4e3a\u6574\u6570<code>123<\/code>\u3002<strong>\u786e\u4fdd\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u8f6c\u6362<\/strong>\u8fd9\u4e00\u70b9\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c<code>int()<\/code>\u51fd\u6570\u5c06\u629b\u51fa<code>ValueError<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u786e\u4fdd\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8f6c\u6362\u5b57\u7b26\u4e32\u4e4b\u524d\uff0c\u5fc5\u987b\u786e\u4fdd\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5426\u5219\u8f6c\u6362\u5c06\u5931\u8d25\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u5982<code>.isdigit()<\/code>\u6765\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>num_str = &quot;123a&quot;<\/p>\n<p>if num_str.isdigit():<\/p>\n<p>    num_int = int(num_str)<\/p>\n<p>    print(num_int)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>isdigit()<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u3002\u5982\u679c\u4e0d\u7b26\u5408\u6761\u4ef6\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6d6e\u70b9\u6570\u7684\u622a\u65ad<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5904\u7406\u6d6e\u70b9\u6570\u65f6\uff0c\u51fd\u6570\u5c06\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u4ec5\u4fdd\u7559\u6574\u6570\u90e8\u5206\u3002\u8fd9\u79cd\u7279\u6027\u5728\u5904\u7406\u6d6e\u70b9\u6570\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>num_float = 123.456<\/p>\n<p>num_int = int(num_float)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6d6e\u70b9\u6570<code>123.456<\/code>\u88ab\u8f6c\u6362\u4e3a\u6574\u6570<code>123<\/code>\uff0c\u5c0f\u6570\u90e8\u5206\u88ab\u622a\u65ad\u3002<strong>\u8fd9\u79cd\u622a\u65ad\u662f\u76f4\u63a5\u4e22\u5f03\u5c0f\u6570\u90e8\u5206<\/strong>\uff0c\u800c\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff0c\u6240\u4ee5\u5728\u8fdb\u884c\u8fd9\u79cd\u8f6c\u6362\u65f6\u9700\u8981\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u51b3\u5b9a\u662f\u5426\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u662f\u826f\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u3002Python\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5e94\u5bf9\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>num_str = &quot;123a&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(num_str)<\/p>\n<p>    print(num_int)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8f6c\u6362\u5931\u8d25\uff1a\u8f93\u5165\u4e0d\u4e3a\u6709\u6548\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u7684<code>ValueError<\/code>\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u8f93\u5165\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u811a\u672c\u7f16\u5199\u4e2d\uff0c\u7c7b\u578b\u8f6c\u6362\u662f\u5e38\u89c1\u7684\u9700\u6c42\u3002<strong>\u7406\u89e3\u548c\u638c\u63e1\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u7684\u6280\u5de7\uff0c\u80fd\u591f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u9002\u5e94\u6027<\/strong>\u3002\u8fd9\u79cd\u6280\u80fd\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u89e3\u6790\u6587\u4ef6\u5185\u5bb9\u4ee5\u53ca\u4e0e\u5176\u4ed6\u7cfb\u7edf\u63a5\u53e3\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5c06\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u5347\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u8fd8\u80fd\u6709\u6548\u907f\u514d\u56e0\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>int(&quot;123&quot;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570<code>123<\/code>\u3002\u6ce8\u610f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u53ea\u5305\u542b\u6570\u5b57\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u8981\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u81ea\u52a8\u622a\u65ad\u5c0f\u6570\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>int(3.7)<\/code>\u7684\u7ed3\u679c\u5c06\u662f<code>3<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u8f6c\u6362\u4e0d\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u800c\u662f\u76f4\u63a5\u53d6\u6574\u6570\u90e8\u5206\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u5728\u53d8\u91cf\u8d4b\u503c\u65f6\u6539\u53d8\u7c7b\u578b\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5728\u8d4b\u503c\u65f6\u76f4\u63a5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>my_var = int(&quot;456&quot;)<\/code>\u5c06\u5b57\u7b26\u4e32<code>&quot;456&quot;<\/code>\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf<code>my_var<\/code>\u3002\u8fd9\u4f7f\u5f97\u5728\u6570\u636e\u5904\u7406\u65f6\u80fd\u591f\u5feb\u901f\u8f6c\u5316\u6570\u636e\u7c7b\u578b\uff0c\u65b9\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u4e2d\u7684\u53d8\u91cf\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684int()\u51fd\u6570\u3002\u4f7f\u7528int()\u51fd\u6570\u3001\u786e\u4fdd\u5b57\u7b26\u4e32\u53ef\u4ee5\u88ab\u8f6c\u6362\u3001\u5904 [&hellip;]","protected":false},"author":3,"featured_media":971047,"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\/971042"}],"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=971042"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971042\/revisions"}],"predecessor-version":[{"id":971052,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971042\/revisions\/971052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971047"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}