{"id":921663,"date":"2024-12-26T14:17:04","date_gmt":"2024-12-26T06:17:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/921663.html"},"modified":"2024-12-26T14:17:06","modified_gmt":"2024-12-26T06:17:06","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/921663.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210527\/f3cc7347-a202-44d5-8969-320977622f7b.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u6587\u4ef6\u5199\u5165\u6765\u5b9e\u73b0\u3002<\/strong> \u4f7f\u7528<code>print()<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u663e\u793a\u7ed3\u679c\u3002\u65e5\u5fd7\u8bb0\u5f55\u5219\u9002\u7528\u4e8e\u9700\u8981\u8ddf\u8e2a\u548c\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u72b6\u6001\u7684\u60c5\u51b5\u3002\u6587\u4ef6\u5199\u5165\u5219\u7528\u4e8e\u5c06\u8f93\u51fa\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>print()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u8f93\u51fa\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u4fe1\u606f\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u65f6\u67e5\u770b\u7a0b\u5e8f\u7684\u8f93\u51fa\u548c\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>print()<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u5373\u53ef\u3002\u53ef\u4ee5\u8f93\u51fa\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u53d8\u91cf\u7684\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u51fa\u5b57\u7b26\u4e32<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u6570\u5b57<\/strong><\/h2>\n<p>print(123)<\/p>\n<h2><strong>\u8f93\u51fa\u53d8\u91cf<\/strong><\/h2>\n<p>name = &quot;Alice&quot;<\/p>\n<p>print(name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u591a\u4e2a\u53c2\u6570\u8f93\u51fa<\/h4>\n<\/p>\n<p><p><code>print()<\/code>\u51fd\u6570\u652f\u6301\u8f93\u51fa\u591a\u4e2a\u53c2\u6570\uff0c\u53c2\u6570\u4e4b\u95f4\u9ed8\u8ba4\u7528\u7a7a\u683c\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Name:&quot;, name, &quot;Age:&quot;, 25)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u7684\u8f93\u51fa\u65b9\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>f-string<\/code>\u3001<code>str.format()<\/code>\u3001\u6216\u65e7\u5f0f\u7684<code>%<\/code>\u683c\u5f0f\u5316\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528f-string<\/p>\n<p>print(f&quot;My name is {name} and I am {25} years old.&quot;)<\/p>\n<h2><strong>\u4f7f\u7528str.format()<\/strong><\/h2>\n<p>print(&quot;My name is {} and I am {} years old.&quot;.format(name, 25))<\/p>\n<h2><strong>\u4f7f\u7528\u65e7\u5f0f%\u683c\u5f0f\u5316<\/strong><\/h2>\n<p>print(&quot;My name is %s and I am %d years old.&quot; % (name, 25))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u72b6\u6001\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>logging<\/code>\u6a21\u5757\u3002\u76f8\u6bd4\u4e8e<code>print()<\/code>\uff0c\u65e5\u5fd7\u8bb0\u5f55\u63d0\u4f9b\u4e86\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\u548c\u591a\u79cd\u65e5\u5fd7\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>logging<\/code>\u6a21\u5757\uff0c\u5e76\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f<\/strong><\/h2>\n<p>logging.basicConfig(level=logging.DEBUG, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u591a\u4e2a\u51fd\u6570\u6765\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u5982<code>debug()<\/code>, <code>info()<\/code>, <code>warning()<\/code>, <code>error()<\/code>, \u548c<code>critical()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logging.debug(&quot;This is a debug message.&quot;)<\/p>\n<p>logging.info(&quot;This is an info message.&quot;)<\/p>\n<p>logging.warning(&quot;This is a warning message.&quot;)<\/p>\n<p>logging.error(&quot;This is an error message.&quot;)<\/p>\n<p>logging.critical(&quot;This is a critical message.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5c06\u7a0b\u5e8f\u7684\u8f93\u51fa\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u4ee5\u4fbf\u540e\u7eed\u67e5\u770b\u548c\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u6253\u5f00\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u6307\u5b9a\u5199\u5165\u6a21\u5f0f\u3002\u5e38\u7528\u7684\u6a21\u5f0f\u6709<code>w<\/code>\uff08\u5199\u5165\uff09\u548c<code>a<\/code>\uff08\u8ffd\u52a0\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u4ee5\u5199\u5165\u6a21\u5f0f<\/p>\n<p>file = open(&quot;output.txt&quot;, &quot;w&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5199\u5165\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&quot;Hello, World!\\n&quot;)<\/p>\n<p>file.write(f&quot;My name is {name} and I am {25} years old.\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5173\u95ed\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5b8c\u6210\u5199\u5165\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6587\u4ef6\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8f93\u51fa\u5230\u6587\u4ef6\u5e76\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u65e5\u5fd7\u8bb0\u5f55\u548c\u6587\u4ef6\u8f93\u51fa\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u4fe1\u606f\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u4fbf\u4fdd\u5b58\u548c\u67e5\u770b\u8be6\u7ec6\u7684\u7a0b\u5e8f\u8fd0\u884c\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>file_handler = logging.FileHandler(&#39;logfile.log&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f<\/strong><\/h2>\n<p>formatter = logging.Formatter(&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>file_handler.setFormatter(formatter)<\/p>\n<h2><strong>\u83b7\u53d6\u65e5\u5fd7\u8bb0\u5f55\u5668<\/strong><\/h2>\n<p>logger = logging.getLogger()<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b<\/strong><\/h2>\n<p>logger.setLevel(logging.DEBUG)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u4ef6\u5904\u7406\u5668<\/strong><\/h2>\n<p>logger.addHandler(file_handler)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">logger.debug(&quot;This is a debug message.&quot;)<\/p>\n<p>logger.info(&quot;This is an info message.&quot;)<\/p>\n<p>logger.warning(&quot;This is a warning message.&quot;)<\/p>\n<p>logger.error(&quot;This is an error message.&quot;)<\/p>\n<p>logger.critical(&quot;This is a critical message.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u51fa\u65b9\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u63a7\u5236\u53f0\u8f93\u51fa\u3001\u8be6\u7ec6\u7684\u65e5\u5fd7\u8bb0\u5f55\uff0c\u8fd8\u662f\u6587\u4ef6\u5199\u5165\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u8f93\u51fa\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u8f93\u51fa\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u8c03\u8bd5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u8f93\u51fa\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u4fe1\u606f\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u548c\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello, World!&quot;)<\/code>\u5c06\u6253\u5370\u51fa&quot;Hello, World!&quot;\u3002\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u4f8b\u5982\uff1a<code>name = &quot;Alice&quot;; print(f&quot;Hello, {name}!&quot;)<\/code>\uff0c\u8fd9\u4f1a\u8f93\u51fa&quot;Hello, Alice!&quot;\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u8f93\u51fa\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u7684<code>print()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f93\u51fa\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002\u65e0\u8bba\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u6570\u636e\uff0c\u53ea\u8981\u5c06\u5176\u4f20\u9012\u7ed9<code>print()<\/code>\u51fd\u6570\uff0c\u90fd\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u3002\u4f8b\u5982\uff0c<code>print([1, 2, 3])<\/code>\u4f1a\u8f93\u51fa\u5217\u8868\u5185\u5bb9<code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49Python\u8f93\u51fa\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6216\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u6765\u5b9a\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-string\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d2\u5165\u53d8\u91cf\uff1a<code>value = 42; print(f&quot;The answer is {value}.&quot;)<\/code>\u3002\u53e6\u5916\uff0c<code>print()<\/code>\u51fd\u6570\u8fd8\u652f\u6301<code>sep<\/code>\u548c<code>end<\/code>\u53c2\u6570\uff0c\u5141\u8bb8\u7528\u6237\u5b9a\u4e49\u8f93\u51fa\u9879\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u548c\u8f93\u51fa\u7ed3\u675f\u540e\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello&quot;, &quot;World&quot;, sep=&quot;, &quot;, end=&quot;!&quot;)<\/code>\u5c06\u8f93\u51fa&quot;Hello, World!&quot;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528print()\u51fd\u6570\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u6587\u4ef6\u5199\u5165\u6765\u5b9e\u73b0\u3002 \u4f7f\u7528print()\u662f\u6700\u5e38\u7528\u7684 [&hellip;]","protected":false},"author":3,"featured_media":921667,"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\/921663"}],"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=921663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921663\/revisions"}],"predecessor-version":[{"id":921669,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921663\/revisions\/921669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/921667"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=921663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=921663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=921663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}