{"id":1091861,"date":"2025-01-08T14:15:43","date_gmt":"2025-01-08T06:15:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091861.html"},"modified":"2025-01-08T14:15:45","modified_gmt":"2025-01-08T06:15:45","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9print%e5%9c%a8%e4%b8%80%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091861.html","title":{"rendered":"python\u5982\u4f55\u8ba9print\u5728\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205149\/78881ab2-49aa-4145-b22d-52109ad38c49.webp\" alt=\"python\u5982\u4f55\u8ba9print\u5728\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba9print\u5728\u4e00\u884c\u8f93\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528end\u53c2\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u4f7f\u7528sys.stdout.write\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528end\u53c2\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u7684\u7ed3\u5c3e\u5b57\u7b26\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cprint\u51fd\u6570\u5728\u8f93\u51fa\u5185\u5bb9\u540e\u4f1a\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u4e00\u884c\u8f93\u51fa\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eend\u53c2\u6570\u6765\u6539\u53d8\u8fd9\u4e00\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528end\u53c2\u6570\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5728\u8c03\u7528print\u51fd\u6570\u65f6\uff0c\u901a\u8fc7\u4f20\u9012end\u53c2\u6570\u5e76\u8bbe\u7f6e\u5176\u503c\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u6216\u5176\u5b83\u975e\u6362\u884c\u7b26\u53f7\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u8fde\u7eed\u8f93\u51fa\u800c\u4e0d\u6362\u884c\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello&quot;, end=&quot; &quot;)<\/code>\u5c06\u8f93\u51fa&quot;Hello &quot;\uff0c\u800c\u4e0d\u662f&quot;Hello\\n&quot;\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u5728\u540c\u4e00\u884c\u8f93\u51fa\u591a\u4e2a\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528end\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528end\u53c2\u6570\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cprint\u51fd\u6570\u4f1a\u5728\u8f93\u51fa\u5185\u5bb9\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u5982\u679c\u4e0d\u5e0c\u671b\u6362\u884c\uff0c\u53ef\u4ee5\u5c06end\u53c2\u6570\u8bbe\u7f6e\u4e3a\u5176\u4ed6\u5b57\u7b26\uff0c\u5982\u7a7a\u5b57\u7b26\u4e32\u6216\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;, end=&quot; &quot;)<\/p>\n<p>print(&quot;World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cend\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u7a7a\u683c\uff0c\u56e0\u6b64\uff0c&quot;Hello&quot;\u548c&quot;World!&quot;\u5c06\u88ab\u8f93\u51fa\u5728\u540c\u4e00\u884c\uff0c\u4e2d\u95f4\u7528\u7a7a\u683c\u9694\u5f00\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236print\u51fd\u6570\u5728\u8f93\u51fa\u65f6\u7684\u884c\u4e3a\uff0c\u907f\u514d\u81ea\u52a8\u6362\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528end\u53c2\u6570\uff0c\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6240\u6709\u9700\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4e00\u6b21\u6027\u8f93\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u540c\u4e00\u884c\u8f93\u51fa\u591a\u4e2a\u53d8\u91cf\u6216\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = &quot;Hello&quot;<\/p>\n<p>b = &quot;World&quot;<\/p>\n<p>print(a + &quot; &quot; + b + &quot;!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u52a0\u53f7\u5c06\u5b57\u7b26\u4e32a\u3001\u4e00\u4e2a\u7a7a\u683c\u5b57\u7b26\u4e32\u3001\u5b57\u7b26\u4e32b\u548c\u4e00\u4e2a\u611f\u53f9\u53f7\u8fde\u63a5\u8d77\u6765\uff0c\u7136\u540e\u4e00\u6b21\u6027\u4f20\u9012\u7ed9print\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u6240\u6709\u5185\u5bb9\u90fd\u5c06\u8f93\u51fa\u5728\u540c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528sys.stdout.write<\/p>\n<\/p>\n<p><p>sys.stdout.write\u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u6362\u884c\u8f93\u51fa\u7684\u65b9\u6cd5\u3002\u4e0eprint\u51fd\u6570\u4e0d\u540c\uff0csys.stdout.write\u4e0d\u4f1a\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u56e0\u6b64\u9700\u8981\u624b\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff08\u5982\u679c\u9700\u8981\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.stdout.write(&quot;Hello &quot;)<\/p>\n<p>sys.stdout.write(&quot;World!\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528sys.stdout.write\u51fd\u6570\u5206\u522b\u8f93\u51fa&quot;Hello &quot;\u548c&quot;World!\\n&quot;\u3002\u7531\u4e8e\u6ca1\u6709\u81ea\u52a8\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u5c06\u4f4d\u4e8e\u540c\u4e00\u884c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0csys.stdout.write\u7684\u53c2\u6570\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5982%\u64cd\u4f5c\u7b26\u3001str.format\u65b9\u6cd5\u548cf-string\uff08Python 3.6\u53ca\u4ee5\u4e0a\uff09\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u591a\u4e2a\u53d8\u91cf\u683c\u5f0f\u5316\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4e00\u6b21\u6027\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = &quot;Hello&quot;<\/p>\n<p>b = &quot;World&quot;<\/p>\n<p>print(&quot;{} {}&quot;.format(a, b))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528str.format\u65b9\u6cd5\u5c06\u53d8\u91cfa\u548cb\u683c\u5f0f\u5316\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4e00\u6b21\u6027\u8f93\u51fa\u3002\u8fd9\u6837\uff0c\u6240\u6709\u5185\u5bb9\u90fd\u5c06\u8f93\u51fa\u5728\u540c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u9891\u7e41\u5730\u5728\u540c\u4e00\u884c\u8f93\u51fa\u591a\u4e2a\u53d8\u91cf\uff0c\u4f7f\u7528end\u53c2\u6570\u53ef\u80fd\u66f4\u4e3a\u7b80\u6d01\u548c\u65b9\u4fbf\uff1b\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u6216\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e0d\u6362\u884c\u8f93\u51fa\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u56e0\u6b64\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5e94\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u4ee3\u7801\u89c4\u8303\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0print\u5728\u4e00\u884c\u8f93\u51fa\uff0c\u5305\u62ec\u4f7f\u7528end\u53c2\u6570\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u3001sys.stdout.write\u548c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u89c4\u8303\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u7684\u8f93\u51fa\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5355\u884c\u6253\u5370\u591a\u4e2a\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u7684<code>end<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u5355\u884c\u6253\u5370\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>print()<\/code>\u51fd\u6570\u5728\u8f93\u51fa\u540e\u6dfb\u52a0\u6362\u884c\u7b26\uff0c\u4f46\u901a\u8fc7\u8bbe\u7f6e<code>end<\/code>\u53c2\u6570\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff08<code>&#39;&#39;<\/code>\uff09\uff0c\u53ef\u4ee5\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;Hello&quot;, end=&#39; &#39;)\nprint(&quot;World&quot;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa<code>Hello World<\/code>\u5728\u540c\u4e00\u884c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u901a\u8fc7\u9017\u53f7\u5206\u9694\u8f93\u51fa\u800c\u4e0d\u6362\u884c\uff1f<\/strong><br \/>\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u9017\u53f7\u6765\u5206\u9694\u591a\u4e2a\u8f93\u51fa\u9879\u3002\u6bcf\u4e2a\u8f93\u51fa\u9879\u4e4b\u95f4\u4f1a\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a\u7a7a\u683c\uff0c\u800c\u4e0d\u6362\u884c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;Hello&quot;, &quot;World&quot;, &quot;Python&quot;, sep=&#39;, &#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa<code>Hello, World, Python<\/code>\uff0c\u5404\u4e2a\u8f93\u51fa\u9879\u4e4b\u95f4\u4ee5\u9017\u53f7\u548c\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b9e\u73b0\u5355\u884c\u8f93\u51fa\uff1f<\/strong><br \/>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08\u5982f-string\uff09\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u5355\u884c\u8f93\u51fa\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u6253\u5370\u51fa\u6765\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = &quot;Alice&quot;\nage = 30\nprint(f&quot;{name} is {age} years old.&quot;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a<code>Alice is 30 years old.<\/code>\uff0c\u6240\u6709\u4fe1\u606f\u6574\u9f50\u5730\u5728\u4e00\u884c\u4e2d\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba9print\u5728\u4e00\u884c\u8f93\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528end\u53c2\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u4f7f\u7528sys.stdout.w [&hellip;]","protected":false},"author":3,"featured_media":1091866,"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\/1091861"}],"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=1091861"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091861\/revisions"}],"predecessor-version":[{"id":1091870,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091861\/revisions\/1091870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091866"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}