{"id":1096177,"date":"2025-01-08T14:58:40","date_gmt":"2025-01-08T06:58:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1096177.html"},"modified":"2025-01-08T14:58:43","modified_gmt":"2025-01-08T06:58:43","slug":"python%e4%bd%a0%e5%a6%82%e4%bd%95%e5%9c%a8str%e5%90%8e%e5%8a%a0%e5%8f%98%e9%87%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1096177.html","title":{"rendered":"python\u4f60\u5982\u4f55\u5728str\u540e\u52a0\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211525\/e6118970-9df0-41b9-a5f1-8e2384479cb8.webp\" alt=\"python\u4f60\u5982\u4f55\u5728str\u540e\u52a0\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5c06\u53d8\u91cf\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5\u3001f-strings\u3001<code>format()<\/code>\u65b9\u6cd5\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u52a0\u53f7\u8fde\u63a5\u3001f-strings\u3001<code>format()<\/code>\u65b9\u6cd5<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u8fd9\u91cc\u5bf9f-strings\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09\uff0c\u662fPython 3.6\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd\u201cf\u201d\u6216\u201cF\u201d\uff0c\u5e76\u4f7f\u7528\u5927\u62ec\u53f7 {} \u5305\u56f4\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\u6765\u5c06\u5176\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002f-strings\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\uff0c\u56e0\u800c\u53d7\u5230\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u52a0\u53f7\u8fde\u63a5<\/h2>\n<\/p>\n<p><p>\u52a0\u53f7\u8fde\u63a5\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u4f7f\u7528\u52a0\u53f7 <code>+<\/code> \u5c06\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u8fde\u63a5\u8d77\u6765\u3002\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\uff0c\u4f46\u5728\u5904\u7406\u8f83\u5927\u6216\u8f83\u590d\u6742\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = &quot;Hello, &quot; + name + &quot;!&quot;<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001f-strings<\/h2>\n<\/p>\n<p><p>f-strings \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd <code>f<\/code>\uff0c\u5e76\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u5305\u542b\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u5c06\u53d8\u91cf\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = f&quot;Hello, {name}!&quot;<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-strings \u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u652f\u6301\u5728\u5927\u62ec\u53f7\u5185\u76f4\u63a5\u6267\u884c\u8868\u8fbe\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>info = f&quot;{name} is {age} years old.&quot;<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001<code>format()<\/code>\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>format()<\/code> \u65b9\u6cd5\u662f Python 2.7 \u548c 3.0 \u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u7136\u540e\u8c03\u7528\u5b57\u7b26\u4e32\u7684 <code>format()<\/code> \u65b9\u6cd5\uff0c\u5c06\u53d8\u91cf\u4f20\u5165\u76f8\u5e94\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = &quot;Hello, {}!&quot;.format(name)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>format()<\/code> \u65b9\u6cd5\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u5b83\u652f\u6301\u901a\u8fc7\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>info = &quot;{name} is {age} years old.&quot;.format(name=name, age=age)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662f Python \u4e2d\u8f83\u65e9\u671f\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528\u767e\u5206\u53f7 <code>%<\/code> \u548c\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u6307\u5b9a\u53d8\u91cf\u7684\u4f4d\u7f6e\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728 Python 3 \u4e2d\u4ecd\u7136\u53ef\u7528\uff0c\u4f46\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>greeting = &quot;Hello, %s!&quot; % name<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u8fd8\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u591a\u4e2a\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = 25<\/p>\n<p>info = &quot;%s is %d years old.&quot; % (name, age)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\u662f\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7 <code>string<\/code> \u6a21\u5757\u4e2d\u7684 <code>Template<\/code> \u7c7b\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u5360\u4f4d\u7b26\u6216\u5bf9\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u8fdb\u884c\u66f4\u591a\u63a7\u5236\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>template = Template(&quot;Hello, $name!&quot;)<\/p>\n<p>greeting = template.substitute(name=name)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u5b83\u652f\u6301\u5728\u5360\u4f4d\u7b26\u4e2d\u4f7f\u7528\u9ed8\u8ba4\u503c\u6216\u5176\u4ed6\u66ff\u6362\u89c4\u5219\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">template = Template(&quot;$name is $age years old.&quot;)<\/p>\n<p>info = template.substitute(name=name, age=25)<\/p>\n<p>print(info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u53d8\u91cf\uff0c\u5305\u62ec\u52a0\u53f7\u8fde\u63a5\u3001f-strings\u3001<code>format()<\/code> \u65b9\u6cd5\u3001\u767e\u5206\u53f7\u683c\u5f0f\u5316\u548c\u6a21\u677f\u5b57\u7b26\u4e32\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u548c\u4ee3\u7801\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><p><strong>f-strings \u662f\u76ee\u524d\u6700\u63a8\u8350\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u6613\u8bfb\u4e14\u6027\u80fd\u8f83\u597d\u3002<\/strong> \u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f Python 3.6 \u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u5efa\u8bae\u4f18\u5148\u8003\u8651\u4f7f\u7528 f-strings \u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u53d8\u91cf\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u53d8\u91cf\u9644\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08\u5982f-string\uff09\u548c\u5b57\u7b26\u4e32\u7684join()\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528<code>new_string = original_string + variable<\/code>\u5c06\u53d8\u91cf\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u3002\u5982\u679c\u4f7f\u7528f-string\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff0c\u683c\u5f0f\u4e3a<code>f&quot;{original_string}{variable}&quot;<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5408\u5e76\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\uff1f<\/strong><br \/>\u9664\u4e86\u52a0\u53f7\u548cf-string\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u5408\u5e76\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u4f8b\u5982<code>&quot;{}{}&quot;.format(original_string, variable)<\/code>\u3002\u6b64\u5916\uff0c<code>join()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u793a\u4f8b\u4e3a<code>&#39;&#39;.join([original_string, variable])<\/code>\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u53d8\u91cf\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5c06\u53d8\u91cf\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\u65f6\uff0c\u786e\u4fdd\u53d8\u91cf\u7684\u7c7b\u578b\u662f\u5b57\u7b26\u4e32\u3002\u5982\u679c\u53d8\u91cf\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u6216\u5217\u8868\uff09\uff0c\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str(variable)<\/code>\u6765\u5b9e\u73b0\u3002\u6b64\u5916\uff0c\u8981\u6ce8\u610f\u7a7a\u683c\u548c\u5176\u4ed6\u5b57\u7b26\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u5408\u5e76\u540e\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5c06\u53d8\u91cf\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5\u3001f-string [&hellip;]","protected":false},"author":3,"featured_media":1096186,"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\/1096177"}],"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=1096177"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096177\/revisions"}],"predecessor-version":[{"id":1096192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1096177\/revisions\/1096192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1096186"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1096177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1096177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1096177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}