{"id":1135436,"date":"2025-01-08T21:25:24","date_gmt":"2025-01-08T13:25:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135436.html"},"modified":"2025-01-08T21:25:27","modified_gmt":"2025-01-08T13:25:27","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%87%e6%8d%a2%e5%88%b0%e4%b8%8b%e4%b8%80%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135436.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u5207\u6362\u5230\u4e0b\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104044\/ffb3114d-8a6f-481c-b3ce-2a4f5018f502.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u5207\u6362\u5230\u4e0b\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5207\u6362\u5230\u4e0b\u4e00\u884c\u7684\u4e3b\u8981\u65b9\u5f0f\u6709\u4f7f\u7528\u6362\u884c\u7b26\u3001\u4f7f\u7528\u4e09\u5f15\u53f7\u591a\u884c\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u5728\u4ee3\u7801\u7f16\u5199\u4e2d\u5229\u7528\u53cd\u659c\u6760\u8fdb\u884c\u884c\u7eed\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u6362\u884c\u7b26\uff08\\n\uff09\uff0c\u5b83\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u884c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u6362\u884c\u7b26\uff08\\n\uff09<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u521b\u5efa\u65b0\u7684\u884c\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u6253\u5370\u4e00\u4e2a\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0cPython\u4f1a\u5728\u9047\u5230\\n\u65f6\u5c06\u5185\u5bb9\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello,\\nworld!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u672c\u5206\u6210\u591a\u884c\uff0c\u5e76\u4f7f\u5176\u5728\u8f93\u51fa\u65f6\u5448\u73b0\u4e3a\u591a\u884c\u683c\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728Python\u4e2d\u5207\u6362\u5230\u4e0b\u4e00\u884c\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u6362\u884c\u7b26\uff08\\n\uff09<\/strong><\/h2>\n<p><p>\u6362\u884c\u7b26\uff08\\n\uff09\u662f\u6700\u5e38\u7528\u7684\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u5207\u6362\u5230\u4e0b\u4e00\u884c\u7684\u7b26\u53f7\u3002\u5b83\u662f\u4e00\u4e2a\u7279\u6b8a\u5b57\u7b26\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u4ee3\u8868\u65b0\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u5728\u6253\u5370\u8f93\u51fa\u4e2d\u4f7f\u7528\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5728\u6253\u5370\u8f93\u51fa\u65f6\u5c06\u5185\u5bb9\u5206\u6210\u591a\u884c\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\\n\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello,\\nworld!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.2 \u5728\u53d8\u91cf\u8d4b\u503c\u4e2d\u4f7f\u7528\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p>\u4e0d\u4ec5\u4ec5\u662f\u5728\u6253\u5370\u65f6\uff0c\u5728\u53d8\u91cf\u8d4b\u503c\u65f6\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">message = &quot;Hello,\\nworld!&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\u4e0e\u4e0a\u4e00\u4e2a\u793a\u4f8b\u76f8\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>1.3 \u5728\u957f\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5904\u7406\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528\u6362\u884c\u7b26\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u591a\u884c\uff0c\u589e\u5f3a\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">long_string = &quot;This is a very long string that we want to split\\ninto multiple lines for better readability.&quot;<\/p>\n<p>print(long_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>This is a very long string that we want to split<\/p>\n<p>into multiple lines for better readability.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4e09\u5f15\u53f7\u591a\u884c\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p><p>Python\u8fd8\u652f\u6301\u4f7f\u7528\u4e09\u5f15\u53f7\uff08&#39;&#39;&#39; \u6216 &quot;&quot;&quot;\uff09\u6765\u8868\u793a\u591a\u884c\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u8f7b\u677e\u6362\u884c\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u4f7f\u7528\u4e09\u5f15\u53f7\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u4e09\u5f15\u53f7\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u975e\u5e38\u7b80\u5355\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multi_line_string = &quot;&quot;&quot;This is a string<\/p>\n<p>that spans multiple<\/p>\n<p>lines.&quot;&quot;&quot;<\/p>\n<p>print(multi_line_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>This is a string<\/p>\n<p>that spans multiple<\/p>\n<p>lines.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2 \u4fdd\u7559\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u4e09\u5f15\u53f7\u4e0d\u4ec5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u8f7b\u677e\u6362\u884c\uff0c\u8fd8\u53ef\u4ee5\u4fdd\u7559\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">poem = &quot;&quot;&quot;Roses are red,<\/p>\n<p>Violets are blue,<\/p>\n<p>Python is great,<\/p>\n<p>And so are you.&quot;&quot;&quot;<\/p>\n<p>print(poem)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Roses are red,<\/p>\n<p>Violets are blue,<\/p>\n<p>Python is great,<\/p>\n<p>And so are you.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u53cd\u659c\u6760\u884c\u7eed<\/strong><\/h2>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5982\u679c\u4e00\u884c\u4ee3\u7801\u8fc7\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u6765\u5c06\u5176\u5206\u6210\u591a\u884c\u3002\u8fd9\u79f0\u4e3a\u884c\u7eed\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u4f7f\u7528\u53cd\u659c\u6760\u8fdb\u884c\u884c\u7eed<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u884c\u4ee3\u7801\u5206\u6210\u591a\u884c\u65f6\uff0c\u53ef\u4ee5\u5728\u884c\u5c3e\u4f7f\u7528\u53cd\u659c\u6760\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">long_expression = 1 + 2 + 3 + 4 + 5 + \\<\/p>\n<p>                  6 + 7 + 8 + 9 + 10<\/p>\n<p>print(long_expression)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2 \u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u53cd\u659c\u6760<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u53c2\u6570\u5217\u8868\u8fc7\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\u5c06\u5176\u5206\u6210\u591a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = some_function(arg1, arg2, arg3, arg4, arg5, \\<\/p>\n<p>                       arg6, arg7, arg8, arg9, arg10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f7f\u957f\u884c\u4ee3\u7801\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u62ec\u53f7<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u5728\u5706\u62ec\u53f7\u3001\u65b9\u62ec\u53f7\u6216\u82b1\u62ec\u53f7\u5185\u7f16\u5199\u4ee3\u7801\uff0c\u53ef\u4ee5\u4e0d\u4f7f\u7528\u53cd\u659c\u6760\u76f4\u63a5\u6362\u884c\u3002\u8fd9\u662f\u56e0\u4e3aPython\u4f1a\u81ea\u52a8\u5c06\u62ec\u53f7\u5185\u7684\u5185\u5bb9\u89c6\u4e3a\u4e00\u4e2a\u6574\u4f53\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u5706\u62ec\u53f7<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u53c2\u6570\u5217\u8868\u8fc7\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5706\u62ec\u53f7\u5c06\u5176\u5206\u6210\u591a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = some_function(<\/p>\n<p>    arg1, arg2, arg3, arg4, arg5,<\/p>\n<p>    arg6, arg7, arg8, arg9, arg10<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u4f7f\u7528\u53cd\u659c\u6760\u7c7b\u4f3c\uff0c\u4f46\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h2>4.2 \u5728\u5217\u8868\u5b9a\u4e49\u4e2d\u4f7f\u7528\u65b9\u62ec\u53f7<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u65f6\uff0c\u5982\u679c\u5217\u8868\u8fc7\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7\u5c06\u5176\u5206\u6210\u591a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [<\/p>\n<p>    1, 2, 3, 4, 5,<\/p>\n<p>    6, 7, 8, 9, 10<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u957f\u5217\u8868\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h2>4.3 \u5728\u5b57\u5178\u5b9a\u4e49\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u65f6\uff0c\u5982\u679c\u5b57\u5178\u8fc7\u957f\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7\u5c06\u5176\u5206\u6210\u591a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {<\/p>\n<p>    &#39;key1&#39;: &#39;value1&#39;,<\/p>\n<p>    &#39;key2&#39;: &#39;value2&#39;,<\/p>\n<p>    &#39;key3&#39;: &#39;value3&#39;,<\/p>\n<p>    &#39;key4&#39;: &#39;value4&#39;,<\/p>\n<p>    &#39;key5&#39;: &#39;value5&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u957f\u5b57\u5178\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5728\u4e00\u8d77\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u548cjoin()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\u62fc\u63a5\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">part1 = &quot;Hello, &quot;<\/p>\n<p>part2 = &quot;world!&quot;<\/p>\n<p>full_message = part1 + part2<\/p>\n<p>print(full_message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.2 \u4f7f\u7528join()\u65b9\u6cd5\u62fc\u63a5\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>join()\u65b9\u6cd5\u662f\u4e00\u4e2a\u66f4\u52a0\u9ad8\u6548\u7684\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parts = [&quot;Hello&quot;, &quot;world!&quot;]<\/p>\n<p>full_message = &quot; &quot;.join(parts)<\/p>\n<p>print(full_message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5\u548cf\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>message = &quot;My name is %s and I am %d years old.&quot; % (name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>My name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.2 \u4f7f\u7528str.format()\u65b9\u6cd5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u73b0\u4ee3\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>message = &quot;My name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>My name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.3 \u4f7f\u7528f\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u6700\u4e3a\u7b80\u6d01\u548c\u73b0\u4ee3\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>message = f&quot;My name is {name} and I am {age} years old.&quot;<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>My name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6362\u884c<\/strong><\/h2>\n<p><p>\u5f53\u4f60\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u6362\u884c\u7b26\u4e5f\u8d77\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002\u4f60\u9700\u8981\u77e5\u9053\u5982\u4f55\u8bfb\u53d6\u548c\u5199\u5165\u5305\u542b\u6362\u884c\u7b26\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>7.1 \u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cexample.txt\u6587\u4ef6\u5185\u5bb9\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>7.2 \u5199\u5165\u6587\u4ef6\u4e2d\u7684\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p>\u5f53\u4f60\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6362\u884c\u7b26\u5c06\u5185\u5bb9\u5206\u6210\u591a\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello,\\nworld!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u5c06\u5185\u5bb9\u5199\u5165example.txt\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u5305\u542b\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h2>7.3 \u4f7f\u7528writelines()\u65b9\u6cd5\u5199\u5165\u591a\u884c<\/h2>\n<\/p>\n<p><p>writelines()\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5199\u5165\u6587\u4ef6\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b57\u7b26\u4e32\u540e\u6dfb\u52a0\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;Hello,&quot;, &quot;world!&quot;]<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.writelines(line + &quot;\\n&quot; for line in lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u5c06\u5185\u5bb9\u5199\u5165example.txt\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u6bcf\u884c\u540e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26<\/strong><\/h2>\n<p><p>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4f7f\u7528\u4e0d\u540c\u7684\u6362\u884c\u7b26\u3002Windows\u4f7f\u7528\\r\\n\uff0cUnix\u548cLinux\u4f7f\u7528\\n\uff0cMac OS\u4f7f\u7528\\r\u3002Python\u63d0\u4f9b\u4e86\u5de5\u5177\u6765\u5904\u7406\u8fd9\u4e9b\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h2>8.1 \u4f7f\u7528universal newline mode<\/h2>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528universal newline mode\uff08\u6a21\u5f0f\uff09\u6765\u81ea\u52a8\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;, newline=None) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6362\u884c\u7b26\uff0c\u65e0\u8bba\u6587\u4ef6\u662f\u5728\u54ea\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u521b\u5efa\u7684\u3002<\/p>\n<\/p>\n<p><h2>8.2 \u4f7f\u7528os.linesep<\/h2>\n<\/p>\n<p><p>os.linesep\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>print(repr(os.linesep))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e0a\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>&#39;\\r\\n&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u800c\u5728Unix\u6216Linux\u4e0a\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>&#39;\\n&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528os.linesep\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u4ee3\u7801\uff0c\u81ea\u52a8\u9002\u5e94\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u683c\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u5728\u5b57\u7b26\u4e32\u6a21\u677f\u4e2d\u4f7f\u7528\u6362\u884c\u7b26<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u6a21\u677f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Template\u7c7b\u6765\u521b\u5efa\u5305\u542b\u53d8\u91cf\u548c\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>9.1 \u521b\u5efa\u5b57\u7b26\u4e32\u6a21\u677f<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Template\u7c7b\u521b\u5efa\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u6a21\u677f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Hello,\\n$name!&quot;)<\/p>\n<p>message = template.substitute(name=&quot;world&quot;)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>9.2 \u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f\u5904\u7406\u591a\u884c\u6587\u672c<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6a21\u677f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u591a\u884c\u6587\u672c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">template_text = &quot;&quot;&quot;Roses are red,<\/p>\n<p>Violets are blue,<\/p>\n<p>$name is great,<\/p>\n<p>And so are you.&quot;&quot;&quot;<\/p>\n<p>template = Template(template_text)<\/p>\n<p>message = template.substitute(name=&quot;Python&quot;)<\/p>\n<p>print(message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Roses are red,<\/p>\n<p>Violets are blue,<\/p>\n<p>Python is great,<\/p>\n<p>And so are you.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u5207\u6362\u5230\u4e0b\u4e00\u884c\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6362\u884c\u7b26\uff08\\n\uff09\uff0c\u5b83\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u521b\u5efa\u65b0\u7684\u884c\u3002\u6b64\u5916\uff0c\u4e09\u5f15\u53f7\u591a\u884c\u5b57\u7b26\u4e32\u3001\u53cd\u659c\u6760\u884c\u7eed\u3001\u62ec\u53f7\u4e2d\u7684\u6362\u884c\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u6587\u4ef6\u5904\u7406\u4e2d\u7684\u6362\u884c\u3001\u5904\u7406\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u3001\u548c\u5b57\u7b26\u4e32\u6a21\u677f\u7b49\u65b9\u6cd5\u4e5f\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5de5\u5177\u6765\u5904\u7406\u591a\u884c\u6587\u672c\u3002\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u6362\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u659c\u6760\uff08\\\uff09\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u6362\u884c\u3002\u6b64\u5916\uff0c\u5b57\u7b26\u4e32\u4e2d\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<code>\\n<\/code>\u6765\u8868\u793a\u6362\u884c\uff0c\u4fbf\u4e8e\u5728\u6253\u5370\u8f93\u51fa\u4e2d\u663e\u793a\u591a\u884c\u6587\u672c\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello\\nWorld&quot;)<\/code>\u5c06\u8f93\u51fa\u4e3a\uff1a  <\/p>\n<pre><code>Hello\nWorld\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u7684\u591a\u884c\u5b57\u7b26\u4e32\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/strong><br \/>\u591a\u884c\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u4e09\u4e2a\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u81ea\u7136\u5730\u5305\u542b\u6362\u884c\u7b26\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;\u8fd9\u662f\u4e00\u6bb5\n\u591a\u884c\u5b57\u7b26\u4e32&quot;&quot;&quot;\nprint(multiline_string)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u4fdd\u7559\u6362\u884c\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u5e76\u5904\u7406\u6362\u884c\uff1f<\/strong><br \/>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>readline()<\/code>\u6216<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u3002\u6bcf\u884c\u7684\u672b\u5c3e\u901a\u5e38\u4f1a\u5305\u542b\u6362\u884c\u7b26\uff0c\u53ef\u4ee5\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u9010\u884c\u5904\u7406\u6587\u4ef6\u5185\u5bb9\uff0c\u540c\u65f6\u53bb\u6389\u591a\u4f59\u7684\u6362\u884c\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5207\u6362\u5230\u4e0b\u4e00\u884c\u7684\u4e3b\u8981\u65b9\u5f0f\u6709\u4f7f\u7528\u6362\u884c\u7b26\u3001\u4f7f\u7528\u4e09\u5f15\u53f7\u591a\u884c\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u5728\u4ee3\u7801\u7f16\u5199\u4e2d\u5229\u7528\u53cd\u659c\u6760\u8fdb\u884c\u884c\u7eed [&hellip;]","protected":false},"author":3,"featured_media":1135446,"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\/1135436"}],"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=1135436"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135436\/revisions"}],"predecessor-version":[{"id":1135449,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135436\/revisions\/1135449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135446"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}