{"id":940286,"date":"2024-12-26T20:57:02","date_gmt":"2024-12-26T12:57:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940286.html"},"modified":"2024-12-26T20:57:06","modified_gmt":"2024-12-26T12:57:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940286.html","title":{"rendered":"python\u4e2d\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\/25075004\/7f87c272-e8c7-4879-b6fe-f430d52546f8.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u51fa\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8f93\u51fa\u4fe1\u606f\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528<strong>print()\u51fd\u6570<\/strong>\u3001<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u548c<strong>\u65e5\u5fd7\u6a21\u5757<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u8f93\u51fa\u6587\u672c\u4fe1\u606f\u3001\u53d8\u91cf\u503c\u548c\u8c03\u8bd5\u4fe1\u606f\u53d8\u5f97\u7b80\u5355\u800c\u9ad8\u6548\u3002<strong>print()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u80fd\u591f\u76f4\u63a5\u5c06\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u975e\u5e38\u9002\u5408\u5728\u521d\u5b66\u8005\u9636\u6bb5\u6216\u7b80\u5355\u7684\u811a\u672c\u4e2d\u4f7f\u7528\u3002\u5bf9\u4e8e\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u60c5\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5982f-strings\u6216<code>str.format()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u8f93\u51fa\u7684\u5185\u5bb9\u66f4\u52a0\u6613\u8bfb\u548c\u7f8e\u89c2\u3002\u800c\u5728\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8bb0\u5f55\u8fd0\u884c\u4fe1\u606f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<strong>\u65e5\u5fd7\u6a21\u5757<\/strong>\uff08logging\uff09\u662f\u6700\u4f73\u5b9e\u8df5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>print()\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u8f93\u51fa\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5c06\u4fe1\u606f\u76f4\u63a5\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u652f\u6301\u8f93\u51fa\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u53d8\u91cf\u4ee5\u53ca\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002print()\u51fd\u6570\u7684\u4f7f\u7528\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\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p>print(42)<\/p>\n<p>print(&quot;The answer is&quot;, 42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>print()\u51fd\u6570\u8fd8\u652f\u6301\u591a\u4e2a\u53c2\u6570\uff0cPython\u4f1a\u5c06\u8fd9\u4e9b\u53c2\u6570\u6309\u987a\u5e8f\u8f93\u51fa\uff0c\u5e76\u5728\u53c2\u6570\u4e4b\u95f4\u52a0\u4e0a\u4e00\u4e2a\u7a7a\u683c\u3002\u6b64\u5916\uff0cprint()\u51fd\u6570\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>sep<\/code>\u548c<code>end<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\u3002<code>sep<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u591a\u4e2a\u53c2\u6570\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u800c<code>end<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8f93\u51fa\u7ed3\u675f\u65f6\u7684\u5b57\u7b26\uff08\u9ed8\u8ba4\u662f\u6362\u884c\u7b26\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u8f93\u51fa\u7684\u5185\u5bb9\u66f4\u52a0\u6574\u9f50\u7f8e\u89c2\uff0c\u7279\u522b\u662f\u5728\u8f93\u51fa\u53d8\u91cf\u503c\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u65e7\u5f0f\u7684\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316\u3001\u65b0\u5f0f\u7684<code>str.format()<\/code>\u65b9\u6cd5\u548c\u6700\u65b0\u7684f-strings\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u767e\u5206\u53f7 (%) \u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u8fd9\u662fPython\u4e2d\u6700\u65e9\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u653e\u7f6e\u683c\u5f0f\u7b26\u53f7\uff08\u5982%s, %d\u7b49\uff09\u6765\u8868\u793a\u66ff\u6362\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u767e\u5206\u53f7\u64cd\u4f5c\u7b26\u5c06\u503c\u63d2\u5165\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: %s, Age: %d&quot; % (name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001str.format() \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.format()<\/code>\u65b9\u6cd5\u662fPython 3\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7{}\u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u901a\u8fc7format\u65b9\u6cd5\u4f20\u9012\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<code>str.format()<\/code>\u65b9\u6cd5\u8fd8\u652f\u6301\u6307\u5b9a\u4f4d\u7f6e\u3001\u547d\u540d\u53c2\u6570\u4ee5\u53ca\u683c\u5f0f\u5316\u5bf9\u9f50\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3\u3001f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h4>\n<\/p>\n<p><p>f-strings\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f7f\u7528\u524d\u7f00<code>f<\/code>\u6216<code>F<\/code>\u7d27\u8ddf\u5728\u5b57\u7b26\u4e32\u4e4b\u524d\u3002\u5728f-strings\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u82b1\u62ec\u53f7\u5185\u5d4c\u5165\u8868\u8fbe\u5f0f\u6216\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 35<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-strings\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u800c\u4e14\u6548\u7387\u66f4\u9ad8\uff0c\u662f\u63a8\u8350\u4f7f\u7528\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u6216\u9700\u8981\u8bb0\u5f55\u8fd0\u884c\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\uff08logging\uff09\u662f\u6700\u4f73\u5b9e\u8df5\u3002\u65e5\u5fd7\u6a21\u5757\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165logging\u6a21\u5757\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aLogger\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528Logger\u5bf9\u8c61\u7684\u65b9\u6cd5\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>logger = logging.getLogger(__name__)<\/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><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u65e5\u5fd7\u7ea7\u522b<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u6a21\u5757\u63d0\u4f9b\u4e86\u4e94\u4e2a\u6807\u51c6\u65e5\u5fd7\u7ea7\u522b\uff0c\u4ece\u4f4e\u5230\u9ad8\u5206\u522b\u662fDEBUG\u3001INFO\u3001WARNING\u3001ERROR\u548cCRITICAL\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u6765\u63a7\u5236\u8f93\u51fa\u54ea\u4e9b\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u81ea\u5b9a\u4e49\u65e5\u5fd7\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u6a21\u5757\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u65e5\u5fd7\u914d\u7f6e\uff0c\u5305\u62ec\u65e5\u5fd7\u683c\u5f0f\u3001\u8f93\u51fa\u76ee\u6807\uff08\u5982\u6587\u4ef6\u3001\u63a7\u5236\u53f0\uff09\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7<code>logging.basicConfig()<\/code>\u51fd\u6570\u6216\u66f4\u9ad8\u7ea7\u7684\u914d\u7f6e\u65b9\u6cd5\u8fdb\u884c\u81ea\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logging.basicConfig(<\/p>\n<p>    level=logging.DEBUG,<\/p>\n<p>    format=&#39;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#39;,<\/p>\n<p>    handlers=[<\/p>\n<p>        logging.FileHandler(&quot;debug.log&quot;),<\/p>\n<p>        logging.StreamHandler()<\/p>\n<p>    ]<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u4fe1\u606f\u7684\u65b9\u5f0f\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u638c\u63e1print()\u51fd\u6570\u548c\u57fa\u672c\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5df2\u7ecf\u8db3\u591f\uff0c\u800c\u5728\u590d\u6742\u7684\u9879\u76ee\u4e2d\uff0c\u5b66\u4e60\u4f7f\u7528\u65e5\u5fd7\u6a21\u5757\u5c06\u4e3a\u7a0b\u5e8f\u7684\u8c03\u8bd5\u548c\u7ef4\u62a4\u5e26\u6765\u5f88\u5927\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8f93\u51fa\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8f93\u51fa\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5217\u8868\u7b49\u3002\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u8f93\u51fa\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>print(&quot;Hello, World!&quot;)<\/code>\uff0c\u82e5\u8981\u8f93\u51fa\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print(42)<\/code>\uff0c\u800c\u8f93\u51fa\u5217\u8868\u5219\u53ef\u4ee5\u4f7f\u7528<code>print([1, 2, 3])<\/code>\u3002\u6240\u6709\u8fd9\u4e9b\u6570\u636e\u90fd\u4f1a\u4ee5\u7528\u6237\u53cb\u597d\u7684\u683c\u5f0f\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f7f\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528f\u5b57\u7b26\u4e32\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u6765\u63d2\u5165\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a<code>name = &quot;Alice&quot;; age = 30; print(f&quot;{name} is {age} years old.&quot;)<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u6216\u767e\u5206\u53f7<code>%<\/code>\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c<code>print(&quot;Hello, %s. You are %d years old.&quot; % (name, age))<\/code>\uff0c\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u80fd\u8ba9\u8f93\u51fa\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6\u800c\u4e0d\u662f\u63a7\u5236\u53f0\uff1f<\/strong><br \/>\u9664\u4e86\u5728\u63a7\u5236\u53f0\u8f93\u51fa\uff0cPython\u4e5f\u53ef\u4ee5\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5199\u5165\u5185\u5bb9\u3002\u4f8b\u5982\uff0c<code>with open(&#39;output.txt&#39;, &#39;w&#39;) as file: file.write(&quot;Hello, World!&quot;)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u8bb0\u5f55\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u4fe1\u606f\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u4f7f\u7528print()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u65e5\u5fd7\u6a21\u5757\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4f7f\u5f97\u8f93\u51fa\u6587\u672c\u4fe1\u606f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":940290,"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\/940286"}],"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=940286"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940286\/revisions"}],"predecessor-version":[{"id":940291,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940286\/revisions\/940291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940290"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}