{"id":1164909,"date":"2025-01-15T15:13:44","date_gmt":"2025-01-15T07:13:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1164909.html"},"modified":"2025-01-15T15:13:46","modified_gmt":"2025-01-15T07:13:46","slug":"python3-6%e5%a6%82%e4%bd%95%e8%bf%9e%e7%bb%ad%e6%89%93%e5%8d%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1164909.html","title":{"rendered":"python3.6\u5982\u4f55\u8fde\u7eed\u6253\u5370"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205408\/31bac74a-3863-4fcb-a329-38c8ee0481c3.webp\" alt=\"python3.6\u5982\u4f55\u8fde\u7eed\u6253\u5370\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python3.6\u8fdb\u884c\u8fde\u7eed\u6253\u5370\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c<strong>print\u51fd\u6570\u7684end\u53c2\u6570<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8bbe\u7f6eend\u53c2\u6570\uff0c\u53ef\u4ee5\u63a7\u5236\u6253\u5370\u8f93\u51fa\u7684\u7ed3\u5c3e\u5b57\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u8fde\u7eed\u6253\u5370\u6548\u679c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5176\u4ed6\u76f8\u5173\u65b9\u6cd5\u7684\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cprint\u51fd\u6570\u9ed8\u8ba4\u5728\u8f93\u51fa\u7ed3\u675f\u540e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff08\u5373&quot;\\n&quot;\uff09\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6bcf\u6b21\u8c03\u7528print\u51fd\u6570\u65f6\uff0c\u8f93\u51fa\u7ed3\u679c\u4f1a\u6362\u884c\u663e\u793a\u3002\u901a\u8fc7\u8bbe\u7f6eprint\u51fd\u6570\u7684end\u53c2\u6570\uff0c\u53ef\u4ee5\u4fee\u6539\u8fd9\u4e00\u884c\u4e3a\uff0c\u5b9e\u73b0\u5728\u540c\u4e00\u884c\u8fde\u7eed\u6253\u5370\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>for i in range(5):<\/p>\n<p>    print(i, end=&#39; &#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cprint\u51fd\u6570\u7684end\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a\u7a7a\u683c\uff08&#39; &#39;\uff09\uff0c\u8fd9\u6837\u6bcf\u6b21\u5faa\u73af\u8f93\u51fa\u7684\u7ed3\u679c\u4e4b\u95f4\u4f1a\u7528\u7a7a\u683c\u9694\u5f00\uff0c\u800c\u4e0d\u4f1a\u6362\u884c\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u5b9e\u73b0\u8fde\u7eed\u6253\u5370<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u6210\u4e00\u4e2a\u6574\u4f53\uff0c\u7136\u540e\u4e00\u6b21\u6027\u8f93\u51fa\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8fde\u7eed\u6253\u5370\u7684\u6548\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5bf9\u6253\u5370\u5185\u5bb9\u8fdb\u884c\u590d\u6742\u683c\u5f0f\u5316\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>output = &#39;&#39;<\/p>\n<p>for i in range(5):<\/p>\n<p>    output += str(i) + &#39; &#39;<\/p>\n<p>print(output.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u5c06\u6bcf\u6b21\u5faa\u73af\u7684\u7ed3\u679c\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u6700\u540e\u901a\u8fc7print\u51fd\u6570\u4e00\u6b21\u6027\u8f93\u51fa\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528sys.stdout.write\u5b9e\u73b0\u8fde\u7eed\u6253\u5370<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528sys\u6a21\u5757\u7684stdout.write\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u5185\u5bb9\uff0c\u907f\u514dprint\u51fd\u6570\u81ea\u5e26\u7684\u6362\u884c\u7b26\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(5):<\/p>\n<p>    sys.stdout.write(str(i) + &#39; &#39;)<\/p>\n<p>sys.stdout.flush()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0csys.stdout.write\u65b9\u6cd5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6807\u51c6\u8f93\u51fa\uff0c\u5e76\u4e14\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\u3002\u901a\u8fc7\u8c03\u7528sys.stdout.flush\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u8f93\u51fa\u5185\u5bb9\u7acb\u5373\u663e\u793a\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u548cjoin\u65b9\u6cd5\u5b9e\u73b0\u8fde\u7eed\u6253\u5370<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5f0f\u7ed3\u5408\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5\uff0c\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u6cd5\uff0c\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>output = &#39; &#39;.join([str(i) for i in range(5)])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5217\u8868\u751f\u6210\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6253\u5370\u5185\u5bb9\u7684\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7join\u65b9\u6cd5\u5c06\u5176\u62fc\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u901a\u8fc7print\u51fd\u6570\u4e00\u6b21\u6027\u8f93\u51fa\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b9e\u73b0\u8fde\u7eed\u6253\u5370<\/h3>\n<\/p>\n<p><p>Python3.6\u5f15\u5165\u4e86f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\uff0c\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>output = &#39; &#39;.join([f&#39;{i}&#39; for i in range(5)])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528f-string\u751f\u6210\u5305\u542b\u683c\u5f0f\u5316\u5185\u5bb9\u7684\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7join\u65b9\u6cd5\u5c06\u5176\u62fc\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u901a\u8fc7print\u51fd\u6570\u4e00\u6b21\u6027\u8f93\u51fa\u3002\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e0d\u540c\u7684\u8fde\u7eed\u6253\u5370\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u9002\u5408\u7b80\u5355\u3001\u5feb\u901f\u7684\u8fde\u7eed\u6253\u5370\u9700\u6c42\uff0c\u800c\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u6216\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5219\u9002\u7528\u4e8e\u590d\u6742\u683c\u5f0f\u5316\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5173\u4e8e\u6027\u80fd\u65b9\u9762\uff0c\u76f4\u63a5\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u548csys.stdout.write\u65b9\u6cd5\u901a\u5e38\u5177\u6709\u8f83\u9ad8\u7684\u6267\u884c\u6548\u7387\uff0c\u56e0\u4e3a\u5b83\u4eec\u907f\u514d\u4e86\u591a\u6b21\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u5f00\u9500\u3002\u5217\u8868\u751f\u6210\u5f0f\u548cjoin\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5927\u6570\u636e\u91cf\u65f6\u4e5f\u8868\u73b0\u826f\u597d\uff0c\u4f46\u5728\u6d89\u53ca\u5927\u91cf\u5faa\u73af\u548c\u62fc\u63a5\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u7565\u900a\u4e8e\u524d\u4e24\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython3.6\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u8fde\u7eed\u6253\u5370\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u3001sys.stdout.write\u3001\u5217\u8868\u751f\u6210\u5f0f\u548cjoin\u65b9\u6cd5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u7f16\u7a0b\u4e2d\u5b9e\u73b0\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u8fde\u7eed\u6253\u5370\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3.6\u4e2d\u5b9e\u73b0\u8fde\u7eed\u6253\u5370\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python 3.6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u8fde\u7eed\u6253\u5370\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u4f7f\u7528<code>for<\/code>\u6216<code>while<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e0a\u53cd\u590d\u6253\u5370\u540c\u4e00\u4fe1\u606f\u6216\u591a\u4e2a\u4fe1\u606f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, i+1, &quot;\u6b21\u6253\u5370&quot;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u6253\u5370\u4e94\u6b21\u201c\u8fd9\u662f\u7b2cX\u6b21\u6253\u5370\u201d\u7684\u6d88\u606f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python 3.6\u8fde\u7eed\u6253\u5370\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8fde\u7eed\u6253\u5370\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u65e0\u9650\u5faa\u73af\u3001\u7f29\u8fdb\u9519\u8bef\u548c\u6253\u5370\u5185\u5bb9\u683c\u5f0f\u4e0d\u5f53\u3002\u786e\u4fdd\u8bbe\u7f6e\u5faa\u73af\u7684\u7ec8\u6b62\u6761\u4ef6\uff0c\u907f\u514d\u65e0\u9650\u5faa\u73af\u7684\u53d1\u751f\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u4ee3\u7801\u7684\u7f29\u8fdb\uff0cPython\u5bf9\u7f29\u8fdb\u975e\u5e38\u654f\u611f\uff0c\u7f29\u8fdb\u9519\u8bef\u5c06\u5bfc\u81f4\u4ee3\u7801\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p><strong>\u5728Python 3.6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u63a7\u5236\u6253\u5370\u7684\u95f4\u9694\u65f6\u95f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>sleep<\/code>\u51fd\u6570\u6765\u63a7\u5236\u6253\u5370\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u3002\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6253\u5370\u4fe1\u606f\u4e4b\u95f4\u7684\u5ef6\u8fdf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import time\n\nfor i in range(5):\n    print(&quot;\u6253\u5370\u4fe1\u606f&quot;, i+1)\n    time.sleep(1)  # \u6bcf\u6b21\u6253\u5370\u540e\u6682\u505c1\u79d2\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u6bcf\u6b21\u6253\u5370\u4e4b\u95f4\u6682\u505c1\u79d2\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u6709\u95f4\u9694\u7684\u8fde\u7eed\u6253\u5370\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python3.6\u8fdb\u884c\u8fde\u7eed\u6253\u5370\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528print\u51fd\u6570\u7684end\u53c2\u6570\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1164912,"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\/1164909"}],"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=1164909"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164909\/revisions"}],"predecessor-version":[{"id":1164914,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164909\/revisions\/1164914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1164912"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1164909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1164909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1164909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}