{"id":1094913,"date":"2025-01-08T14:45:47","date_gmt":"2025-01-08T06:45:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094913.html"},"modified":"2025-01-08T14:45:50","modified_gmt":"2025-01-08T06:45:50","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8a%a0%e4%b8%8a%e5%bc%95%e5%8f%b7-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094913.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210824\/5aa6b08a-bd79-42fa-be4c-d6ec21051457.webp\" alt=\"python\u5982\u4f55\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7b49\u3002\u5173\u952e\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528f\u5b57\u7b26\u4e32\u3001\u4f7f\u7528repr\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/strong>\uff1a\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u4f7f\u7528\u53cd\u659c\u6760\u5b57\u7b26\uff08\\\uff09\u6765\u8f6c\u4e49\u5f15\u53f7\uff0c\u4f8b\u5982\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u4f7f\u7528\u53cc\u5f15\u53f7\u65f6\uff0c\u5728\u53cc\u5f15\u53f7\u524d\u52a0\u4e0a\u53cd\u659c\u6760\u3002<strong>\u4f7f\u7528f\u5b57\u7b26\u4e32<\/strong>\uff1a\u501f\u52a9\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u63d2\u5165\u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u53d8\u91cf\u524d\u540e\u52a0\u4e0a\u5f15\u53f7\u3002<strong>\u4f7f\u7528repr\u51fd\u6570<\/strong>\uff1arepr\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u53ef\u6253\u5370\u8868\u793a\u7684\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u4f1a\u5728\u5b57\u7b26\u4e32\u7684\u4e24\u7aef\u52a0\u4e0a\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5728\u5f15\u53f7\u524d\u52a0\u4e0a\u53cd\u659c\u6760\uff08\\\uff09\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u5305\u542b\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u5185\u5305\u542b\u5355\u5f15\u53f7<\/p>\n<p>single_quote_str = &#39;He s<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>d, &quot;Hello, world!&quot;&#39;<\/p>\n<p>print(single_quote_str)<\/p>\n<h2><strong>\u5355\u5f15\u53f7\u5b57\u7b26\u4e32\u5185\u5305\u542b\u53cc\u5f15\u53f7<\/strong><\/h2>\n<p>double_quote_str = &quot;He said, &#39;Hello, world!&#39;&quot;<\/p>\n<p>print(double_quote_str)<\/p>\n<h2><strong>\u5b57\u7b26\u4e32\u5185\u5305\u542b\u53cc\u5f15\u53f7\u548c\u5355\u5f15\u53f7<\/strong><\/h2>\n<p>mixed_quote_str = &quot;He said, \\&quot;It&#39;s a wonderful day!\\&quot;&quot;<\/p>\n<p>print(mixed_quote_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u662fPython 3.6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u63d2\u503c\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u63d2\u5165\u53d8\u91cf\uff0c\u975e\u5e38\u65b9\u4fbf\uff0c\u540c\u65f6\u53ef\u4ee5\u5728\u53d8\u91cf\u524d\u540e\u52a0\u4e0a\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>f_str = f&quot;\\&quot;{name}\\&quot;&quot;<\/p>\n<p>print(f_str)<\/p>\n<p>message = &quot;Hello&quot;<\/p>\n<p>f_message = f&#39;\\&#39;{message}\\&#39;&#39;<\/p>\n<p>print(f_message)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528repr\u51fd\u6570<\/h3>\n<\/p>\n<p><p>repr\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0crepr\u4f1a\u8fd4\u56de\u5e26\u6709\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_value = &quot;Hello, world!&quot;<\/p>\n<p>repr_str = repr(str_value)<\/p>\n<p>print(repr_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u6cd5\uff0c\u5728\u5b57\u7b26\u4e32\u7684\u4e24\u7aef\u52a0\u4e0a\u5f15\u53f7\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u9700\u8981\u6ce8\u610f\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u5426\u542b\u6709\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_value = &quot;Hello, world!&quot;<\/p>\n<p>quoted_str = &#39;&quot;&#39; + str_value + &#39;&quot;&#39;<\/p>\n<p>print(quoted_str)<\/p>\n<p>single_quoted_str = &quot;&#39;&quot; + str_value + &quot;&#39;&quot;<\/p>\n<p>print(single_quoted_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528json.dumps<\/h3>\n<\/p>\n<p><p>json\u6a21\u5757\u7684dumps\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u5b83\u4f1a\u81ea\u52a8\u5728\u4e24\u7aef\u52a0\u4e0a\u53cc\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>str_value = &quot;Hello, world!&quot;<\/p>\n<p>json_str = json.dumps(str_value)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u501f\u52a9\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u7684\u524d\u540e\u63d2\u5165\u5f15\u53f7\u3002\u9700\u8981\u4f7f\u7528re\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>str_value = &quot;Hello, world!&quot;<\/p>\n<p>quoted_str = re.sub(r&quot;(^.*$)&quot;, r&#39;&quot;\\1&quot;&#39;, str_value)<\/p>\n<p>print(quoted_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528join\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684join\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_value = &quot;Hello, world!&quot;<\/p>\n<p>quoted_str = &#39;&#39;.join([&#39;&quot;&#39;, str_value, &#39;&quot;&#39;])<\/p>\n<p>print(quoted_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u975e\u5e38\u7b80\u6d01\u5730\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_value = &quot;Hello, world!&quot;<\/p>\n<p>quoted_str = &#39;&#39;.join([c if c.isalnum() else f&#39;\\&quot;{c}\\&quot;&#39; for c in str_value])<\/p>\n<p>print(quoted_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528StringIO<\/h3>\n<\/p>\n<p><p>StringIO\u6a21\u5757\u7528\u4e8e\u5728\u5185\u5b58\u4e2d\u8bfb\u5199str\u3002\u53ef\u4ee5\u5229\u7528\u5b83\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u5e76\u5728\u4e24\u7aef\u52a0\u4e0a\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from io import StringIO<\/p>\n<p>str_value = &quot;Hello, world!&quot;<\/p>\n<p>output = StringIO()<\/p>\n<p>output.write(&#39;&quot;&#39;)<\/p>\n<p>output.write(str_value)<\/p>\n<p>output.write(&#39;&quot;&#39;)<\/p>\n<p>quoted_str = output.getvalue()<\/p>\n<p>output.close()<\/p>\n<p>print(quoted_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u4e09\u5f15\u53f7<\/h3>\n<\/p>\n<p><p>\u4e09\u5f15\u53f7\u7528\u4e8e\u5305\u542b\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5728\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_value = &#39;&#39;&#39;&quot;Hello, world!&quot;&#39;&#39;&#39;<\/p>\n<p>print(str_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5217\u4e3e\u4e86\u591a\u79cd\u5728Python\u4e2d\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u89e3\u51b3\u5b57\u7b26\u4e32\u5904\u7406\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed9\u5b57\u7b26\u4e32\u6dfb\u52a0\u5f15\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u6765\u5305\u88f9\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u6dfb\u52a0\u5f15\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08\\\uff09\u6765\u907f\u514d\u8bed\u6cd5\u9519\u8bef\u3002\u4f8b\u5982\uff0c<code>my_string = &#39;He said, &quot;Hello!&quot;&#39;<\/code> \u6216 <code>my_string = &quot;It&#39;s a beautiful day.&quot;<\/code>\u3002\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u6765\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u6216\u5305\u542b\u591a\u79cd\u5f15\u53f7\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5904\u7406\u5305\u542b\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u5f15\u53f7\u5305\u88f9\u5b57\u7b26\u4e32\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8f6c\u4e49\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5355\u5f15\u53f7\u5305\u88f9\u5305\u542b\u53cc\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\uff0c\u5982 <code>string_with_quotes = &#39;&quot;Hello&quot;, she said.&#39;<\/code>\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5185\u90e8\u548c\u5916\u90e8\u90fd\u9700\u8981\u4f7f\u7528\u76f8\u540c\u7c7b\u578b\u7684\u5f15\u53f7\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f6c\u4e49\u5b57\u7b26\u5b9e\u73b0\uff0c\u4f8b\u5982\uff0c<code>escaped_string = &#39;It\\&#39;s a sunny day.&#39;<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u52a8\u6001\u7ed9\u5b57\u7b26\u4e32\u6dfb\u52a0\u5f15\u53f7\uff1f<\/strong><br \/>\u52a8\u6001\u7ed9\u5b57\u7b26\u4e32\u6dfb\u52a0\u5f15\u53f7\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u3002\u4f7f\u7528f-string\uff08Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u6216<code>format()<\/code>\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5b8c\u6210\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-string\uff1a<code>name = &quot;Alice&quot;; quoted_string = f&#39;&quot;{name}&quot;&#39;<\/code>\uff0c\u6216\u8005\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\uff1a<code>quoted_string = &#39;&quot;{}&quot;&#39;.format(name)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u53d8\u91cf\u7684\u503c\u52a8\u6001\u751f\u6210\u5e26\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed9\u5b57\u7b26\u4e32\u52a0\u4e0a\u5f15\u53f7\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7b49\u3002\u5173\u952e\u65b9\u6cd5\u5305 [&hellip;]","protected":false},"author":3,"featured_media":1094919,"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\/1094913"}],"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=1094913"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094913\/revisions"}],"predecessor-version":[{"id":1094920,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094913\/revisions\/1094920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094919"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}