{"id":1115696,"date":"2025-01-08T18:09:19","date_gmt":"2025-01-08T10:09:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115696.html"},"modified":"2025-01-08T18:09:21","modified_gmt":"2025-01-08T10:09:21","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e8%a1%8c%e6%89%93%e5%8d%b0%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115696.html","title":{"rendered":"Python\u5982\u4f55\u4e00\u884c\u6253\u5370\u4e00\u4e2a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080336\/40e57137-9787-4392-a658-10a6c39150c4.webp\" alt=\"Python\u5982\u4f55\u4e00\u884c\u6253\u5370\u4e00\u4e2a\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u3002<\/strong> \u8fd9\u91cc\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528print\u51fd\u6570\u6765\u5b9e\u73b0\u4e00\u884c\u6253\u5370\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528print()\u51fd\u6570\u3002print()\u51fd\u6570\u9ed8\u8ba4\u5728\u8f93\u51fa\u5185\u5bb9\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u56e0\u6b64\u6bcf\u6b21\u8c03\u7528print()\u90fd\u4f1a\u5c06\u5185\u5bb9\u6253\u5370\u5728\u65b0\u7684\u4e00\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p>print(&quot;Hello, Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, World!<\/p>\n<p>Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528end\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9aprint()\u51fd\u6570\u7684end\u53c2\u6570\u6765\u63a7\u5236\u8f93\u51fa\u5185\u5bb9\u672b\u5c3e\u7684\u5b57\u7b26\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cend\u53c2\u6570\u7684\u503c\u662f\u6362\u884c\u7b26&#39;\\n&#39;\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u66f4\u6539\u4e3a\u5176\u4ed6\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;, end=&quot; &quot;)<\/p>\n<p>print(&quot;Hello, Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, World! Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u4e24\u6b21print()\u8c03\u7528\u7684\u5185\u5bb9\u5c31\u6253\u5370\u5728\u540c\u4e00\u884c\u4e86\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684print\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u8f93\u51fa\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u5185\u5bb9\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u8c03\u7528print\u51fd\u6570\u90fd\u4f1a\u5728\u8f93\u51fa\u5185\u5bb9\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u4e00\u884c\u6253\u5370\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p>print(&quot;Hello, Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u4e24\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528end\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u3002end\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8f93\u51fa\u5185\u5bb9\u672b\u5c3e\u7684\u5b57\u7b26\uff0c\u9ed8\u8ba4\u503c\u662f\u6362\u884c\u7b26&#39;\\n&#39;\u3002\u901a\u8fc7\u66f4\u6539end\u53c2\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u5185\u5bb9\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;, end=&quot; &quot;)<\/p>\n<p>print(&quot;Hello, Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u4e00\u884c\u5185\u5bb9\uff0c\u56e0\u4e3a\u5c06end\u53c2\u6570\u8bbe\u7f6e\u4e3a\u7a7a\u683c\uff0c\u4f7f\u5f97\u4e24\u6b21print\u51fd\u6570\u8c03\u7528\u7684\u5185\u5bb9\u6253\u5370\u5728\u540c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u7f16\u7a0b\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2.1 for\u5faa\u73af<\/h4>\n<\/p>\n<p><p>for\u5faa\u73af\u662f\u6700\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u904d\u5386\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [&quot;Hello&quot;, &quot;World&quot;, &quot;Hello&quot;, &quot;Python&quot;]<\/p>\n<p>for element in elements:<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u56db\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>2.2 while\u5faa\u73af<\/h4>\n<\/p>\n<p><p>while\u5faa\u73af\u662f\u4e00\u79cd\u6761\u4ef6\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u6761\u4ef6\u4e3a\u771f\u65f6\u53cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u3002\u901a\u8fc7while\u5faa\u73af\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [&quot;Hello&quot;, &quot;World&quot;, &quot;Hello&quot;, &quot;Python&quot;]<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; len(elements):<\/p>\n<p>    print(elements[index])<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u56db\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\u3002\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [f&quot;Element {i}&quot; for i in range(5)]<\/p>\n<p>for element in elements:<\/p>\n<p>    print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u4e94\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528join\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u7b80\u5316\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u4e00\u884c\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [f&quot;Element {i}&quot; for i in range(5)]<\/p>\n<p>print(&quot;\\n&quot;.join(elements))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u4e94\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528print\u51fd\u6570\u548c\u5faa\u73af<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [&quot;Hello&quot;, &quot;World&quot;, &quot;Hello&quot;, &quot;Python&quot;]<\/p>\n<p>for element in elements:<\/p>\n<p>    print(element, end=&quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u56db\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u548cjoin\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [f&quot;Element {i}&quot; for i in range(5)]<\/p>\n<p>print(&quot;\\n&quot;.join(elements))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u8f93\u51fa\u4e94\u884c\u5185\u5bb9\uff0c\u6bcf\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e00\u884c\u6253\u5370\u4e00\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5b9e\u73b0\u7075\u6d3b\u7684\u8f93\u51fa\u63a7\u5236\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e00\u884c\u4ee3\u7801\u6253\u5370\u591a\u4e2a\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u4e00\u6b21\u6027\u6253\u5370\u591a\u4e2a\u53d8\u91cf\u3002\u901a\u8fc7\u5728<code>print()<\/code>\u51fd\u6570\u4e2d\u4ee5\u9017\u53f7\u5206\u9694\u53d8\u91cf\uff0cPython\u4f1a\u81ea\u52a8\u5728\u5b83\u4eec\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\u3002\u4f8b\u5982\uff1a<code>print(var1, var2, var3)<\/code>\u3002\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sep<\/code>\u53c2\u6570\uff0c\u5982<code>print(var1, var2, var3, sep=&#39;-&#39;)<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6253\u5370\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u6253\u5370\u51fa\u66f4\u590d\u6742\u7684\u5185\u5bb9\u3002\u901a\u8fc7f-string\u6216<code>str.format()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-string\u7684\u65b9\u5f0f\uff1a<code>name = &quot;Alice&quot;; age = 30; print(f&quot;Name: {name}, Age: {age}&quot;)<\/code>\uff0c\u6216\u8005\u4f7f\u7528<code>str.format()<\/code>\uff1a<code>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u5e26\u6709\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u6253\u5370\u7684\u5185\u5bb9\u4e2d\u5305\u542b\u6362\u884c\u7b26\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165<code>\\n<\/code>\u3002\u4f8b\u5982\uff1a<code>print(&quot;Hello\\nWorld&quot;)<\/code>\u5c06\u4f1a\u5728\u201cHello\u201d\u548c\u201cWorld\u201d\u4e4b\u95f4\u63d2\u5165\u6362\u884c\u3002\u8981\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u591a\u884c\u6253\u5370\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5e76\u4f7f\u7528<code>\\n<\/code>\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4e00\u884c\u6253\u5370\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u3002 \u8fd9\u91cc\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1115708,"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\/1115696"}],"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=1115696"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115696\/revisions"}],"predecessor-version":[{"id":1115712,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115696\/revisions\/1115712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115708"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}