{"id":1145550,"date":"2025-01-08T23:09:08","date_gmt":"2025-01-08T15:09:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145550.html"},"modified":"2025-01-08T23:09:10","modified_gmt":"2025-01-08T15:09:10","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e8%b7%a8%e8%b6%8a%e5%a4%9a%e8%a1%8c%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145550.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182100\/6f4b9522-282a-424a-863c-b4feef1c35ba.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u3001\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u7eed\u884c\u3001\u7ed3\u5408\u5c0f\u62ec\u53f7\uff08()\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\u6709\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7<\/strong>\u662f\u6700\u5e38\u89c1\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u81ea\u7531\u5730\u4f7f\u7528\u6362\u884c\u7b26\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u3002<strong>\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09<\/strong>\u7eed\u884c\u5219\u9002\u7528\u4e8e\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u4fdd\u6301\u7279\u5b9a\u683c\u5f0f\u7684\u60c5\u51b5\u3002<strong>\u7ed3\u5408\u5c0f\u62ec\u53f7\uff08()\uff09<\/strong>\u7684\u65b9\u6cd5\u5219\u5728\u5904\u7406\u9700\u8981\u8de8\u8d8a\u591a\u884c\u4f46\u4e0d\u5e0c\u671b\u5f15\u5165\u591a\u4f59\u7684\u7a7a\u683c\u6216\u6362\u884c\u7b26\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u5b9a\u4e49\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u81ea\u7531\u5730\u4f7f\u7528\u6362\u884c\u7b26\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u4e32\u4e2d\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;\u8fd9\u662f\u4e00\u4e2a<\/p>\n<p>\u8de8\u8d8a\u591a\u884c\u7684<\/p>\n<p>\u5b57\u7b26\u4e32&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u548c\u5185\u5bb9\u3002\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6362\u884c\u7b26\u548c\u7a7a\u683c\u90fd\u4f1a\u88ab\u4fdd\u7559\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5f15\u5165\u4e0d\u5fc5\u8981\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u7eed\u884c<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u6765\u7eed\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\u4f46\u4e0d\u5e0c\u671b\u5f15\u5165\u591a\u4f59\u7684\u6362\u884c\u7b26\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;\u8fd9\u662f\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\uff0c&quot; \\<\/p>\n<p>                    &quot;\u4f46\u6ca1\u6709\u5f15\u5165\u591a\u4f59\u7684\u6362\u884c\u7b26&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u4fdd\u6301\u5b57\u7b26\u4e32\u7684\u7d27\u51d1\u6027\uff0c\u800c\u4e0d\u4f1a\u5f15\u5165\u591a\u4f59\u7684\u6362\u884c\u7b26\u6216\u7a7a\u683c\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u53ef\u80fd\u4e0d\u5982\u4e09\u91cd\u5f15\u53f7\u65b9\u6cd5\u9ad8\uff0c\u7279\u522b\u662f\u5728\u5b57\u7b26\u4e32\u975e\u5e38\u957f\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u5c0f\u62ec\u53f7\uff08()\uff09<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5c0f\u62ec\u53f7\uff08()\uff09\u7ed3\u5408\u591a\u4e2a\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5728\u4e0d\u5f15\u5165\u591a\u4f59\u7a7a\u683c\u6216\u6362\u884c\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = (&quot;\u8fd9\u662f\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\uff0c&quot;<\/p>\n<p>                     &quot;\u4f46\u6ca1\u6709\u5f15\u5165\u591a\u4f59\u7684\u6362\u884c\u7b26&quot;)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u4fdd\u6301\u4ee3\u7801\u7684\u6574\u6d01\u6027\uff0c\u540c\u65f6\u907f\u514d\u4e86\u53cd\u659c\u6760\u7eed\u884c\u7684\u7f3a\u70b9\u3002\u7136\u800c\uff0c\u4e0e\u53cd\u659c\u6760\u7eed\u884c\u65b9\u6cd5\u4e00\u6837\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u53ef\u80fd\u4e0d\u5982\u4e09\u91cd\u5f15\u53f7\u65b9\u6cd5\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5e38\u7528\uff0c\u4f46\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u5b57\u7b26\u4e32\u8fde\u63a5\u6765\u5b9e\u73b0\u8de8\u8d8a\u591a\u884c\u7684\u6548\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u5b57\u7b26\u4e32\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;\u8fd9\u662f\u4e00\u4e2a&quot; + \\<\/p>\n<p>                    &quot;\u8de8\u8d8a\u591a\u884c\u7684&quot; + \\<\/p>\n<p>                    &quot;\u5b57\u7b26\u4e32&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u65f6\u5019\u52a8\u6001\u5730\u751f\u6210\u5b57\u7b26\u4e32\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u4f4e\uff0c\u800c\u4e14\u5728\u5904\u7406\u957f\u5b57\u7b26\u4e32\u65f6\u663e\u5f97\u6bd4\u8f83\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002Python\u7684<code>string<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Template<\/code>\u7c7b\uff0c\u5141\u8bb8\u4f60\u4f7f\u7528\u5360\u4f4d\u7b26\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u66ff\u6362\u5360\u4f4d\u7b26\u6765\u751f\u6210\u6700\u7ec8\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;\u8fd9\u662f\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u5360\u4f4d\u7b26\uff1a$placeholder&quot;)<\/p>\n<p>multi_line_string = template.substitute(placeholder=&quot;\u793a\u4f8b\u6587\u672c&quot;)<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u9700\u8981\u52a8\u6001\u751f\u6210\u7684\u590d\u6742\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9a\u4e49\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7<\/strong>\u662f\u6700\u76f4\u89c2\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<strong>\u4f7f\u7528\u53cd\u659c\u6760\u7eed\u884c<\/strong>\u548c<strong>\u7ed3\u5408\u5c0f\u62ec\u53f7<\/strong>\u7684\u65b9\u6cd5\u5219\u5728\u9700\u8981\u4fdd\u6301\u5b57\u7b26\u4e32\u7d27\u51d1\u6027\u65f6\u975e\u5e38\u6709\u7528\u3002<strong>\u5b57\u7b26\u4e32\u8fde\u63a5<\/strong>\u548c<strong>\u6a21\u677f\u5b57\u7b26\u4e32<\/strong>\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u4f60\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u7075\u6d3b\u5730\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u591a\u884c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5b57\u7b26\u4e32\u5728\u591a\u884c\u4e0a\u81ea\u7531\u4e66\u5199\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u6362\u884c\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;\u8fd9\u662f\u4e00\u4e2a\n\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\n\u5728Python\u4e2d\u5b9a\u4e49\u3002&quot;&quot;&quot;\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u591a\u884c\u5b57\u7b26\u4e32\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u6ce8\u610f\u5b57\u7b26\u4e32\u7684\u7f29\u8fdb\u548c\u7a7a\u767d\u5b57\u7b26\u3002\u5982\u679c\u5e0c\u671b\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4fdd\u6301\u4e00\u81f4\uff0c\u5efa\u8bae\u5c3d\u91cf\u5c06\u5185\u5bb9\u5bf9\u9f50\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7a7a\u683c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\u6216str.format()\u65b9\u6cd5\u5728\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;\u5f20\u4e09&quot;\ngreeting = f&quot;&quot;&quot;\u4f60\u597d\uff0c{name}\uff01\n\u6b22\u8fce\u4f7f\u7528Python\u3002&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5728\u591a\u884c\u5b57\u7b26\u4e32\u4e2d\u52a8\u6001\u63d2\u5165\u53d8\u91cf\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u3001\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u7eed\u884c\u3001\u7ed3\u5408\u5c0f\u62ec\u53f7\uff08()\uff09 \u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u8de8\u8d8a\u591a\u884c\u7684\u5b57\u7b26\u4e32\u6709\u51e0\u79cd\u4e0d\u540c\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1145555,"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\/1145550"}],"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=1145550"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145550\/revisions"}],"predecessor-version":[{"id":1145557,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145550\/revisions\/1145557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145555"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}