{"id":179239,"date":"2024-05-08T20:08:55","date_gmt":"2024-05-08T12:08:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179239.html"},"modified":"2024-05-08T20:09:06","modified_gmt":"2024-05-08T12:09:06","slug":"python%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0int%e3%80%81float%e3%80%81str%e7%9a%84%e7%94%a8%e6%b3%95%e5%88%86%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179239.html","title":{"rendered":"python\u4e2d\u7684\u51fd\u6570int\u3001float\u3001str\u7684\u7528\u6cd5\u5206\u522b\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063954\/412af95e-5396-40df-948d-87a9d24f8ee3.webp\" alt=\"python\u4e2d\u7684\u51fd\u6570int\u3001float\u3001str\u7684\u7528\u6cd5\u5206\u522b\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong><code>int<\/code>\u3001<code>float<\/code>\u3001<code>str<\/code><\/strong> \u662f\u7528\u4e8e\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u5b83\u4eec\u5206\u522b\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<strong><code>int<\/code><\/strong> \u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u5b57\u6216\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\u4e2d\u751f\u6210\u4e00\u4e2a\u6574\u6570\uff0c<strong><code>float<\/code><\/strong> \u51fd\u6570\u5219\u7528\u6765\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u6570\u5b57\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u800c <strong><code>str<\/code><\/strong> \u51fd\u6570\u7528\u4e8e\u5c06\u7ed9\u5b9a\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001INT\u51fd\u6570\u7684\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><strong><code>int<\/code><\/strong> \u51fd\u6570\u7528\u4e8e\u5c06\u4e00\u4e2a\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7eaf\u6574\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6570\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53ef\u9009\u53c2\u6570\u4e3a\u8fdb\u5236\u6570\uff0c\u9ed8\u8ba4\u4e3a10\uff0c\u8868\u793a\u5341\u8fdb\u5236\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4f7f\u7528<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6570\u5b57\u8f6c\u6362<\/p>\n<p>num = 123.45<\/p>\n<p>integer_num = int(num)  # \u7ed3\u679c\u662f 123<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362<\/strong><\/h2>\n<p>str_num = &quot;456&quot;<\/p>\n<p>integer_str = int(str_num)  # \u7ed3\u679c\u662f 456<\/p>\n<h2><strong>\u4f7f\u7528\u8fdb\u5236\u8f6c\u6362<\/strong><\/h2>\n<p>hex_str = &quot;1A&quot;<\/p>\n<p>integer_hex = int(hex_str, 16)  # \u7ed3\u679c\u662f 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5f53\u4f20\u5165\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4f1a\u629b\u51fa ValueError \u5f02\u5e38\u3002\u4f7f\u7528\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7528\u5230\u5f02\u5e38\u5904\u7406\u6765\u907f\u514d\u7a0b\u5e8f\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001FLOAT\u51fd\u6570\u7684\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><strong><code>float<\/code><\/strong> \u51fd\u6570\u7528\u6765\u5c06\u4e00\u4e2a\u6574\u6570\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5f53\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u65f6\uff0c\u5b57\u7b26\u4e32\u9700\u8981\u8868\u793a\u4e00\u4e2a\u5408\u6cd5\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4f7f\u7528<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6574\u6570\u8f6c\u6362<\/p>\n<p>int_num = 123<\/p>\n<p>float_num = float(int_num)  # \u7ed3\u679c\u662f 123.0<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362<\/strong><\/h2>\n<p>str_num = &quot;123.456&quot;<\/p>\n<p>float_str = float(str_num)  # \u7ed3\u679c\u662f 123.456<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e<code>int<\/code>\uff0c\u82e5\u4f20\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u662f\u4e00\u4e2a\u5408\u6cd5\u7684\u6d6e\u70b9\u6570\u8868\u8ff0\uff0c\u4f7f\u7528<code>float<\/code>\u51fd\u6570\u4e5f\u4f1a\u629b\u51fa ValueError \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001STR\u51fd\u6570\u7684\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><strong><code>str<\/code><\/strong> \u51fd\u6570\u53ef\u4ee5\u5c06\u51e0\u4e4e\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002\u65e0\u8bba\u662f\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u751a\u81f3\u662f\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u90fd\u53ef\u4ee5\u7528<code>str<\/code>\u51fd\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u4fbf\u4e8e\u6253\u5370\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4f7f\u7528<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u6570\u5b57\u8f6c\u6362<\/p>\n<p>num = 123<\/p>\n<p>str_num = str(num)  # \u7ed3\u679c\u662f &#039;123&#039;<\/p>\n<h2><strong>\u4ece\u5217\u8868\u8f6c\u6362<\/strong><\/h2>\n<p>list_data = [1, 2, 3]<\/p>\n<p>str_list = str(list_data)  # \u7ed3\u679c\u662f &#039;[1, 2, 3]&#039;<\/p>\n<h2><strong>\u4ece\u81ea\u5b9a\u4e49\u5bf9\u8c61\u8f6c\u6362<\/strong><\/h2>\n<p>class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 5<\/p>\n<p>my_obj = MyClass()<\/p>\n<p>str_obj = str(my_obj)  # \u7ed3\u679c\u6839\u636e\u5bf9\u8c61\u7684 __str__ \u6216 __repr__ \u65b9\u6cd5\u800c\u5b9a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u8fd0\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u8fd9\u4e9b\u51fd\u6570\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u8f93\u5165\u8f93\u51fa\u5904\u7406\u65f6\u3001\u5728\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u8f6c\u5316\u503c\u65f6\u7b49\u573a\u666f\uff0c\u4f60\u5fc5\u987b\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u8bfb\u51fa\u7684\u6570\u636e\u8f6c\u6362\u6210\u9002\u5f53\u7684\u6570\u503c\u7c7b\u578b\u6765\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\uff0c\u6216\u8005\u5728\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u524d\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u62fc\u63a5\u4e0e\u663e\u793a<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u6216\u6570\u636e\u663e\u793a\u5230\u7ec8\u7aef\u65f6\uff0c\u9700\u8981\u5c06\u6570\u5b57\u7b49\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ee5\u514d\u7c7b\u578b\u9519\u8bef\u6216\u8005\u662f\u4e3a\u4e86\u66f4\u597d\u5730\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u8f93\u5165\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u7528\u6237\u4ece\u63a7\u5236\u53f0\u8f93\u5165\u7684\u6570\u636e\u901a\u5e38\u662f\u5b57\u7b26\u4e32\uff0c\u5bf9\u4e8e\u9700\u8981\u6570\u503c\u578b\u7684\u5904\u7406\u573a\u666f\uff0c\u901a\u8fc7<code>int<\/code>\u548c<code>float<\/code>\u51fd\u6570\u8f6c\u6362\u624d\u80fd\u8fdb\u884c\u540e\u7eed\u7684\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u8fdb\u884c\u6570\u636e\u8f6c\u6362\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4fdd\u8bc1\u8f6c\u6362\u524d\u7684\u6570\u636e\u662f\u5408\u6cd5\u7684\uff0c\u6bd4\u5982\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u662f\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u7684\u3002<\/li>\n<li>\u5728\u8f6c\u6362\u5927\u91cf\u6570\u636e\u65f6\u8981\u8003\u8651\u6548\u7387\u95ee\u9898\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\u3002<\/li>\n<li>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u5e94\u5bf9\u7528\u6237\u8f93\u5165\u6216\u6570\u636e\u6e90\u4e2d\u7684\u4e0d\u5408\u6cd5\u6570\u636e\u3002<\/li>\n<li>\u7406\u89e3\u6bcf\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\uff0c\u4f8b\u5982<code>float<\/code>\u51fd\u6570\u4e0d\u4f1a\u8fd4\u56de\u6574\u6570\uff0c\u5373\u4f7f\u5c0f\u6570\u90e8\u5206\u4e3a0\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u4ee3\u7801\u7684\u5065\u58ee\u6027\u4e5f\u662f\u975e\u5e38\u5173\u952e\u7684\u3002\u56e0\u6b64\u61c2\u5f97\u6b63\u786e\u5730\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5904\u7406\u5f02\u5e38\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347\u7a0b\u5e8f\u7684\u8fd0\u884c\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: Python\u4e2d\u7684int\u51fd\u6570\u7528\u6765\u505a\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>A: int\u51fd\u6570\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4e22\u5f03\u5c0f\u6570\u90e8\u5206\u5e76\u4fdd\u7559\u6574\u6570\u90e8\u5206\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5c06\u5176\u820d\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528int(&#039;10&#039;)\u5c06\u8fd4\u56de\u6574\u657010\uff0c\u800cint(3.14)\u5c06\u8fd4\u56de\u6574\u65703\u3002<\/p>\n<p><strong>Q: float\u51fd\u6570\u5728Python\u4e2d\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><\/p>\n<p>A: float\u51fd\u6570\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u5305\u62ec\u6574\u6570\u5b57\u7b26\u4e32\u548c\u5e26\u6709\u5c0f\u6570\u70b9\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528float(&#039;3.14&#039;)\u5c06\u8fd4\u56de\u6d6e\u70b9\u65703.14\u3002\u6b64\u5916\uff0c\u5b83\u8fd8\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u5982float(5)\u5c06\u8fd4\u56de\u6d6e\u70b9\u65705.0\u3002<\/p>\n<p><strong>Q: \u600e\u6837\u4f7f\u7528str\u51fd\u6570\u6765\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff1f<\/strong><\/p>\n<p>A: str\u51fd\u6570\u5728Python\u4e2d\u7528\u4e8e\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u4ee5\u5c06\u6574\u6570\u6216\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u5982\u5e03\u5c14\u503c\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528str(5)\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32&#039;5&#039;\uff0c\u800cstr(True)\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32&#039;True&#039;\u3002\u6b64\u5916\uff0cstr\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u5c06\u5176\u4ed6\u6570\u636e\u4e0e\u5b57\u7b26\u4e32\u62fc\u63a5\u8d77\u6765\u3002\u4f8b\u5982\uff0c\u4f7f\u7528str(10) + &#039; apples&#039;\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32&#039;10 apples&#039;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cint\u3001float\u3001str \u662f\u7528\u4e8e\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u5b83\u4eec\u5206\u522b\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6574\u6570\u3001\u6d6e\u70b9 [&hellip;]","protected":false},"author":3,"featured_media":179260,"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\/179239"}],"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=179239"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179260"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}