{"id":179370,"date":"2022-04-16T12:07:49","date_gmt":"2022-04-16T04:07:49","guid":{"rendered":"http:\/\/www.idc.net\/help\/179370\/"},"modified":"2022-04-16T12:07:49","modified_gmt":"2022-04-16T04:07:49","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88print%e8%af%ad%e5%8f%a5%e6%89%93%e5%8d%b0%e4%b8%8d%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/179370\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1a\u4e3a\u4ec0\u4e48print\u8bed\u53e5\u6253\u5370\u4e0d\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/004\/5daaa89d6c454549.jpg\" class=\"aligncenter\"><\/p>\n<p>Python\u4e2dprint\u8bed\u53e5\u4e3b\u8981\u662f\u5411\u5c4f\u5e55\u6253\u5370\u8f93\u51fa\u7684\u3002\u5728python2\u548cpython3\u4e2d\uff0c\u4e24\u8005\u662f\u6709\u4e9b\u533a\u522b\u7684\uff0c\u4f7f\u7528\u65f6\u8981\u6ce8\u610f\u4e00\u4e0b\u3002<\/p>\n<p><strong>\u9017\u53f7\u5206\u9694\u7b26<\/strong><\/p>\n<p>\u5728Python\u4e2dprint\u8bed\u53e5\u53ef\u6839\u636esys.stdout\u4e2d\u5305\u542b\u7684\u6587\u4ef6\u751f\u6210\u8f93\u51fa\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u5bf9\u8c61\u8868\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<pre>x&nbsp;=&nbsp;1\ny&nbsp;=&nbsp;2\nz&nbsp;=&nbsp;3\nprint(\"the&nbsp;values&nbsp;are\",&nbsp;x,&nbsp;y,&nbsp;z)\n#\u6253\u5370\u7ed3\u679c\u4e3a&nbsp;the&nbsp;values&nbsp;are&nbsp;1&nbsp;2&nbsp;3<\/pre>\n<p>\u5728\u4e0a\u8ff0\u5b9e\u4f8b\u4e2d\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5bf9\u8c61\uff0c\u5c06\u8c03\u7528str()\u51fd\u6570\u6765\u751f\u6210\u8f93\u51fa\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u8f93\u51fa\u5b57\u7b26\u4e32\u4f1a\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5f7c\u6b64\u4e4b\u95f4\u7528\u4e00\u4e2a\u7a7a\u683c\u5206\u5f00\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u8f93\u51fa\u5b57\u7b26\u4e32\u3002\u8f93\u51fa\u901a\u8fc7\u4e00\u4e2a\u6362\u884c\u7b26\u7ec8\u6b62\u3002<\/p>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/p>\n<p>\u8981\u751f\u6210\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u8fd0\u7b97\u7b26 % \u6216 .format()\u65b9\u6cd5\u3002<\/p>\n<pre>x&nbsp;=&nbsp;1\ny&nbsp;=&nbsp;2\nz&nbsp;=&nbsp;3\nprint(\"the&nbsp;values&nbsp;are&nbsp;%d&nbsp;%7.5f&nbsp;%s\"&nbsp;%&nbsp;(x,&nbsp;y,&nbsp;z))<\/pre>\n<p><strong>\u66f4\u6539print\u8bed\u53e5\u7684\u76ee\u6807<\/strong><\/p>\n<p>\u66f4\u6539print\u8bed\u53e5\u7684\u76ee\u6807\uff0c\u4e5f\u5c31\u662f\u91cd\u5b9a\u5411\u3002\u65b9\u6cd5\u662f\u6dfb\u52a0\u7279\u6b8a\u7684 &gt;&gt;file\u4fee\u9970\u7b26\u548c\u4e00\u4e2a\u9017\u53f7\uff0c\u5176\u4e2dfile\u662f\u4e00\u4e2a\u5141\u8bb8\u5199\u5165\u7684\u6587\u4ef6\u5bf9\u8c61<\/p>\n<pre>f&nbsp;=&nbsp;open(\"file_path\",&nbsp;'w')\nprint&nbsp;&gt;&gt;f,&nbsp;\"hello&nbsp;world\"&nbsp;&nbsp;#\u8fd9\u662fpython2\u4e2d\u7684\u7528\u6cd5\uff0c\u5728python3\u7684\u5177\u4f53\u65b9\u6cd5\u8bf7\u770b\u4e0b\u6587\n...\nf.close()<\/pre>\n<p><strong>print()\u51fd\u6570<\/strong><\/p>\n<p>Python3\u4e2d\u6700\u91cd\u8981\u7684\u66f4\u6539\u4e4b\u4e00\u5c31\u662f\u5c06print\u8f6c\u53d8\u4e3a\u51fd\u6570\uff0c\u5728python2\u4e2d\u4ed6\u662f\u4e00\u4e2a\u5173\u952e\u5b57<\/p>\n<p>\u5176\u7528\u6cd5\u4e0ePython2\u4e2d\u7684\u5927\u81f4\u76f8\u540c\uff0c\u4f46\u662f\u4e00\u4e9b\u7ec6\u679d\u672b\u8282\u8fd8\u662f\u6709\u533a\u522b\u7684\uff0c\u4e0b\u6587\u5c31\u7b80\u8ff0\u8fd9\u4e09\u79cd\u3002<\/p>\n<pre>print(\"the&nbsp;values&nbsp;are\",x,&nbsp;y,&nbsp;z,&nbsp;end&nbsp;=&nbsp;'')&nbsp;#\u7981\u6b62\u6362\u884c\u7b26\nprint(\"the&nbsp;values&nbsp;are\",x,&nbsp;y,&nbsp;z,&nbsp;file&nbsp;=&nbsp;f)&nbsp;#\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u5bf9\u8c61f\nprint(\"the&nbsp;values&nbsp;are\",x,&nbsp;y,&nbsp;z,&nbsp;sep&nbsp;=&nbsp;',')&nbsp;#\u5728\u503c\u4e4b\u95f4\u6dfb\u52a0\u9017\u53f7<\/pre>\n<p>\u66f4\u591a\u5b66\u4e60\u5185\u5bb9\uff0c\u8bf7\u70b9\u51fbpython\u5b66\u4e60\u7f51\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2dprint\u8bed\u53e5\u4e3b\u8981\u662f\u5411\u5c4f\u5e55\u6253\u5370\u8f93\u51fa\u7684\u3002\u5728python2\u548cpython3\u4e2d\uff0c\u4e24\u8005\u662f\u6709\u4e9b\u533a\u522b\u7684\uff0c\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":179371,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-179370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=179370"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179370\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/179371"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=179370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=179370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=179370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}