{"id":1061461,"date":"2024-12-31T15:42:59","date_gmt":"2024-12-31T07:42:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061461.html"},"modified":"2024-12-31T15:43:01","modified_gmt":"2024-12-31T07:43:01","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e5%ad%97%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061461.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/165e2f98-f5b9-476d-917a-e3a4b9372086.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>Python\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570str()\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316\u3001\u4f7f\u7528repr()\u51fd\u6570\u7b49<\/strong>\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684str()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528str()\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u65f6\uff0c\u53ea\u9700\u8981\u7b80\u5355\u5730\u5c06\u6570\u5b57\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9str()\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u6574\u6570123\u8f6c\u6362\u4e3a\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str(123)<\/code>\uff0c\u7ed3\u679c\u4e3a\u5b57\u7b26&#39;123&#39;\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4e14\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u6570\u5b57\uff08\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff09\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u9010\u4e00\u5c55\u5f00\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528str()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684str()\u51fd\u6570\u662f\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002str()\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u8fdb\u5236\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>1.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528str()\u51fd\u6570\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u65f6\uff0c\u53ea\u9700\u5c06\u6574\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9str()\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = str(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0cstr()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = str(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3\u3001\u5904\u7406\u4e0d\u540c\u8fdb\u5236\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684int()\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u4e0d\u540c\u8fdb\u5236\u7684\u6574\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528str()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5c06\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u518d\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_num = &#39;1011&#39;<\/p>\n<p>int_num = int(binary_num, 2)<\/p>\n<p>char_num = str(int_num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;11&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>f-string\u662f\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u76f4\u63a5\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u4f7f\u7528f-string\u683c\u5f0f\u5316\u6570\u5b57\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u76f4\u63a5\u5d4c\u5165\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = f&#39;{num}&#39;<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0cf-string\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = f&#39;{num}&#39;<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528f-string\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u4fdd\u7559\u51e0\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.456<\/p>\n<p>char_num = f&#39;{num:.2f}&#39;<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528repr()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>repr()\u51fd\u6570\u7528\u4e8e\u751f\u6210\u5bf9\u8c61\u7684\u201c\u5b98\u65b9\u201d\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u8be5\u8868\u793a\u901a\u5e38\u5305\u542b\u53ef\u4ee5\u7528\u6765\u91cd\u65b0\u521b\u5efa\u5bf9\u8c61\u7684\u4ee3\u7801\u3002\u5bf9\u4e8e\u6570\u5b57\uff0crepr()\u51fd\u6570\u8fd4\u56de\u6570\u5b57\u7684\u5b57\u7b26\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>3.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = repr(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0crepr()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = repr(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528format()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>format()\u51fd\u6570\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u5e76\u6309\u7167\u6307\u5b9a\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>4.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = format(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0cformat()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = format(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528format()\u51fd\u6570\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u4fdd\u7559\u51e0\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.456<\/p>\n<p>char_num = format(num, &#39;.2f&#39;)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u8f6c\u6362\u64cd\u4f5c\u7b26%<\/h2>\n<\/p>\n<p><p>\u8f6c\u6362\u64cd\u4f5c\u7b26%\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u5e76\u6309\u7167\u6307\u5b9a\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>5.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = &#39;%d&#39; % num<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0c\u8f6c\u6362\u64cd\u4f5c\u7b26%\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = &#39;%f&#39; % num<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.450000&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8f6c\u6362\u64cd\u4f5c\u7b26%\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u4fdd\u7559\u51e0\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.456<\/p>\n<p>char_num = &#39;%.2f&#39; % num<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u8fde\u63a5\u662f\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u4e00\u79cd\u7b80\u5355\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u64cd\u4f5c\u7b26\u5c06\u6570\u5b57\u548c\u7a7a\u5b57\u7b26\u4e32\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h3>6.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = &#39;&#39; + str(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0c\u5b57\u7b26\u4e32\u8fde\u63a5\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = &#39;&#39; + str(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u63d2\u503c\u662f\u5c06\u53d8\u91cf\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7{}\u548cformat()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>7.1\u3001\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123<\/p>\n<p>char_num = &#39;{}&#39;.format(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2\u3001\u6d6e\u70b9\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u7684\uff0c\u5b57\u7b26\u4e32\u63d2\u503c\u4e5f\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.45<\/p>\n<p>char_num = &#39;{}&#39;.format(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.45&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.3\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5c0f\u6570\u70b9\u540e\u4fdd\u7559\u51e0\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 123.456<\/p>\n<p>char_num = &#39;{:.2f}&#39;.format(num)<\/p>\n<p>print(char_num)  # \u8f93\u51fa &#39;123.46&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u573a\u666f\uff0c\u4f8b\u5982\u6570\u636e\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u7528\u6237\u754c\u9762\u5c55\u793a\u7b49\u3002\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0c\u6570\u5b57\u901a\u5e38\u9700\u8981\u4ee5\u5b57\u7b26\u5f62\u5f0f\u5b58\u50a8\u6216\u5c55\u793a\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u6216\u7528\u6237\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><h3>8.1\u3001\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4ee5\u4fbf\u4e8e\u5b58\u50a8\u6216\u4f20\u8f93\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u6570\u5b57\u901a\u5e38\u9700\u8981\u4ee5\u5b57\u7b26\u5f62\u5f0f\u5b58\u50a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 25<\/p>\n<p>}<\/p>\n<p>data_str = json.dumps(data)<\/p>\n<p>print(data_str)  # \u8f93\u51fa &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: &quot;25&quot;}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.2\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u8bb0\u5f55\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4ee5\u4fbf\u4e8e\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u5728\u8bb0\u5f55\u7528\u6237\u64cd\u4f5c\u65e5\u5fd7\u65f6\uff0c\u64cd\u4f5c\u65f6\u95f4\u901a\u5e38\u9700\u8981\u4ee5\u5b57\u7b26\u5f62\u5f0f\u8bb0\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>timestamp = datetime.datetime.now()<\/p>\n<p>log_entry = &#39;User logged in at &#39; + str(timestamp)<\/p>\n<p>print(log_entry)  # \u8f93\u51fa &#39;User logged in at 2023-10-01 12:34:56.789012&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.3\u3001\u7528\u6237\u754c\u9762\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u5728\u7528\u6237\u754c\u9762\u5c55\u793a\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4ee5\u4fbf\u4e8e\u5c55\u793a\u3002\u4f8b\u5982\uff0c\u5728\u5c55\u793a\u7528\u6237\u4fe1\u606f\u65f6\uff0c\u7528\u6237\u5e74\u9f84\u901a\u5e38\u9700\u8981\u4ee5\u5b57\u7b26\u5f62\u5f0f\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>user_info = &#39;User age: &#39; + str(age)<\/p>\n<p>print(user_info)  # \u8f93\u51fa &#39;User age: 25&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528str()\u51fd\u6570\u3001f-string\u683c\u5f0f\u5316\u3001repr()\u51fd\u6570\u3001format()\u51fd\u6570\u3001\u8f6c\u6362\u64cd\u4f5c\u7b26%\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u5b57\u7b26\u4e32\u63d2\u503c\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528str()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u6570\u5b57\u3002f-string\u683c\u5f0f\u5316\u548crepr()\u51fd\u6570\u4e5f\u5f88\u5e38\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u6216\u751f\u6210\u5bf9\u8c61\u7684\u201c\u5b98\u65b9\u201d\u5b57\u7b26\u4e32\u8868\u793a\u65f6\u3002format()\u51fd\u6570\u548c\u8f6c\u6362\u64cd\u4f5c\u7b26%\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u573a\u666f\u3002\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u5b57\u7b26\u4e32\u63d2\u503c\u5219\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u9700\u6c42\uff0c\u63d0\u9ad8Python\u7f16\u7a0b\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>str(123)<\/code>\u5c06\u8fd4\u56de<code>&#39;123&#39;<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\uff0c\u4f8b\u5982\uff0c<code>str(123.45)<\/code>\u5c06\u8fd4\u56de<code>&#39;123.45&#39;<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\uff1f<\/strong><br \/>\u9664\u4e86<code>str()<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u5b57\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\u3002\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>&quot;{:.2f}&quot;.format(123.456)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>&#39;123.46&#39;<\/code>\uff0c\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u6b64\u5916\uff0cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u975e\u5e38\u65b9\u4fbf\uff0c\u4f7f\u7528\u65b9\u5f0f\u5982\uff1a<code>f&quot;{123}&quot;<\/code>\u6216<code>f&quot;{123.456:.2f}&quot;<\/code>\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u80fd\u5426\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff1f<\/strong><br \/>\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u4e0d\u80fd\u76f4\u63a5\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u4e0e\u6570\u5b57\u7c7b\u578b\u4e0d\u540c\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u5fc5\u987b\u5c06\u5b57\u7b26\u4e32\u518d\u8f6c\u6362\u56de\u6570\u5b57\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u4f8b\u5982\uff0c<code>int(&#39;123&#39;)<\/code>\u5c06\u8fd4\u56de<code>123<\/code>\uff0c\u800c<code>float(&#39;123.45&#39;)<\/code>\u5c06\u8fd4\u56de<code>123.45<\/code>\u3002\u786e\u4fdd\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u662f\u6709\u6548\u7684\u6570\u5b57\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570str()\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316\u3001\u4f7f\u7528repr()\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1061470,"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\/1061461"}],"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=1061461"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061461\/revisions"}],"predecessor-version":[{"id":1061473,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061461\/revisions\/1061473"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061470"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}