{"id":1070781,"date":"2025-01-08T11:01:21","date_gmt":"2025-01-08T03:01:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070781.html"},"modified":"2025-01-08T11:01:23","modified_gmt":"2025-01-08T03:01:23","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e5%87%ba%e6%8d%a2%e8%a1%8c%e7%ac%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070781.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u51fa\u6362\u884c\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101431\/e597de95-cf34-44a6-a702-4ffe1efe6d9c.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u51fa\u6362\u884c\u7b26\" \/><\/p>\n<p><p> <strong>Python\u663e\u793a\u6362\u884c\u7b26\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>\\n<\/code>\u3001<code>print()<\/code>\u51fd\u6570\u3001\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\uff08\u591a\u884c\u5b57\u7b26\u4e32\uff09\u7b49<\/strong>\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>\\n<\/code>\u8fdb\u884c\u6362\u884c\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165<code>\\n<\/code>\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u6362\u884c\u7684\u5730\u65b9\u8fdb\u884c\u6362\u884c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>\\n<\/code>\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>\\n<\/code>\u8868\u793a\u6362\u884c\u7b26\uff0c\u5f53\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165<code>\\n<\/code>\u65f6\uff0c\u8f93\u51fa\u65f6\u4f1a\u5728\u8be5\u4f4d\u7f6e\u8fdb\u884c\u6362\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello\\nWorld&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>\\n<\/code>\u4f1a\u5c06<code>Hello<\/code>\u548c<code>World<\/code>\u5206\u6210\u4e24\u884c\u6765\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>print()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>print()<\/code>\u51fd\u6570\u9ed8\u8ba4\u4f1a\u5728\u8f93\u51fa\u7684\u5185\u5bb9\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u591a\u6b21\u8c03\u7528<code>print()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6362\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello&quot;)<\/p>\n<p>print(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6bcf\u6b21\u8f93\u51fa\u7684\u5185\u5bb9\u90fd\u5728\u65b0\u7684\u4e00\u884c\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u4f7f\u7528\u4e09\u5f15\u53f7\uff08<code>&#39;&#39;&#39;<\/code> \u6216 <code>&quot;&quot;&quot;<\/code>\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4f7f\u7528\u4e09\u5f15\u53f7\u53ef\u4ee5\u5c06\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u5b9a\u4e49\u5728\u4ee3\u7801\u4e2d\uff0c\u800c\u65e0\u9700\u624b\u52a8\u63d2\u5165<code>\\n<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;&quot;&quot;Hello<\/p>\n<p>World&quot;&quot;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u6613\u8bfb\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u63d2\u5165\u591a\u4e2a<code>\\n<\/code>\u9020\u6210\u7684\u6df7\u4e71\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u6765\u5c06\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&quot;Hello&quot;, &quot;World&quot;]<\/p>\n<p>print(&quot;\\n&quot;.join(lines))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u9700\u8981\u6362\u884c\u663e\u793a\u7684\u591a\u884c\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>str.format()<\/code>\u6216<code>f-string<\/code><\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63d2\u5165\u6362\u884c\u7b26\u3002<code>str.format()<\/code>\u548cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\u90fd\u652f\u6301\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u5982<code>\\n<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Using str.format()<\/p>\n<p>print(&quot;Hello\\n{}&quot;.format(&quot;World&quot;))<\/p>\n<h2><strong>Using f-string<\/strong><\/h2>\n<p>name = &quot;World&quot;<\/p>\n<p>print(f&quot;Hello\\n{name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>escape sequences<\/code><\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u663e\u793a\u5b9e\u9645\u7684\u6362\u884c\u7b26\u5b57\u7b26\u800c\u4e0d\u662f\u6362\u884c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello\\\\nWorld&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u4f1a\u5728\u8f93\u51fa\u65f6\u663e\u793a<code>Hello\\nWorld<\/code>\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u6362\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5f53\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u6362\u884c\u7b26\u7684\u5904\u7406\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u901a\u5e38\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\u4f1a\u4fdd\u7559\u6362\u884c\u7b26\uff0c\u9664\u975e\u4f60\u8fdb\u884c\u7279\u6b8a\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Writing to a file<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello\\nWorld&quot;)<\/p>\n<h2><strong>Reading from a file<\/strong><\/h2>\n<p>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c<code>read()<\/code>\u65b9\u6cd5\u4f1a\u4fdd\u7559\u6362\u884c\u7b26\uff0c<code>readlines()<\/code>\u65b9\u6cd5\u4f1a\u5c06\u6bcf\u884c\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u4fdd\u7559\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5728GUI\u5e94\u7528\u4e2d\u5904\u7406\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e94\u7528\u65f6\uff0c\u5904\u7406\u6362\u884c\u7b26\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u884c\u6587\u672c\u6846\u6765\u663e\u793a\u5305\u542b\u6362\u884c\u7b26\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>root = tk.Tk()<\/p>\n<p>text = tk.Text(root)<\/p>\n<p>text.insert(tk.END, &quot;Hello\\nWorld&quot;)<\/p>\n<p>text.pack()<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5728Web\u5f00\u53d1\u4e2d\u5904\u7406\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u5904\u7406\u6362\u884c\u7b26\u4e5f\u975e\u5e38\u91cd\u8981\u3002HTML\u4e2d\u7684\u6362\u884c\u7b26\u4e0d\u4f1a\u81ea\u52a8\u663e\u793a\u4e3a\u6362\u884c\uff0c\u5fc5\u987b\u4f7f\u7528<code>&lt;br&gt;<\/code>\u6807\u7b7e\u6216CSS\u6837\u5f0f\u6765\u5b9e\u73b0\u6362\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p&gt;Hello&lt;br&gt;World&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u6362\u884c\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5904\u7406\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6362\u884c\u7b26\u6216\u5339\u914d\u591a\u884c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello\\nWorld&quot;<\/p>\n<h2><strong>Replace \\n with space<\/strong><\/h2>\n<p>new_text = re.sub(r&#39;\\n&#39;, &#39; &#39;, text)<\/p>\n<p>print(new_text)<\/p>\n<h2><strong>Match text across multiple lines<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;Hello.*World&#39;, re.DOTALL)<\/p>\n<p>match = pattern.search(text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Match found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662fPython\u4e2d\u5904\u7406\u548c\u663e\u793a\u6362\u884c\u7b26\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6362\u884c\u7b26\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<code>\\n<\/code>\u6765\u8868\u793a\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>print(&quot;Hello\\nWorld&quot;)<\/code>\u5c06\u6253\u5370\u51fa\uff1a  <\/p>\n<pre><code>Hello  \nWorld\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\uff0c\u80fd\u591f\u5728\u9700\u8981\u65f6\u6709\u6548\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u5e26\u6709\u6362\u884c\u7b26\u7684\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u65f6\uff0cPython\u4f1a\u81ea\u52a8\u5904\u7406\u6362\u884c\u7b26\u3002\u4f7f\u7528<code>with open(&#39;file.txt&#39;, &#39;r&#39;) as file:<\/code>\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u5e76\u9010\u884c\u8bfb\u53d6\u3002\u6bcf\u884c\u7684\u7ed3\u5c3e\u4f1a\u81ea\u52a8\u5305\u542b\u6362\u884c\u7b26\u3002\u53ef\u4ee5\u901a\u8fc7<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u884c\u672b\u7684\u6362\u884c\u7b26\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:\n    for line in file:\n        print(line.strip())\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u591a\u884c\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u4e09\u4e2a\u5f15\u53f7\uff08\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\uff09\u6765\u521b\u5efa\u5b83\u3002\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u4f1a\u4fdd\u7559\u6362\u884c\u7b26\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">multiline_string = &quot;&quot;&quot;This is line one.\nThis is line two.\nThis is line three.&quot;&quot;&quot;\nprint(multiline_string)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u663e\u793a\u6bcf\u884c\u5185\u5bb9\u5404\u81ea\u72ec\u7acb\uff0c\u6362\u884c\u7b26\u5c06\u88ab\u4fdd\u7559\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u663e\u793a\u6362\u884c\u7b26\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\\n\u3001print()\u51fd\u6570\u3001\u4e09\u5f15\u53f7\u5b57\u7b26\u4e32\uff08\u591a\u884c\u5b57\u7b26\u4e32\uff09\u7b49\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f [&hellip;]","protected":false},"author":3,"featured_media":1070788,"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\/1070781"}],"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=1070781"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070781\/revisions"}],"predecessor-version":[{"id":1070791,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070781\/revisions\/1070791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070788"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}