{"id":1072403,"date":"2025-01-08T11:15:51","date_gmt":"2025-01-08T03:15:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072403.html"},"modified":"2025-01-08T11:15:53","modified_gmt":"2025-01-08T03:15:53","slug":"python3%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%9b%bf%e4%bb%a3%e5%8f%98%e9%87%8f-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072403.html","title":{"rendered":"python3\u4e2d\u5982\u4f55\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102623\/72af0ac1-c28b-4b07-b964-4eec8e8914d2.webp\" alt=\"python3\u4e2d\u5982\u4f55\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\" \/><\/p>\n<p><p> \u5728Python3\u4e2d\uff0c\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec<strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f\u5b57\u7b26\u4e32\u3001\u6a21\u677f\u5b57\u7b26\u4e32\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0cf\u5b57\u7b26\u4e32\u662f\u6700\u7b80\u6d01\u4e14\u63a8\u8350\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0af\u6216F\uff0c\u5e76\u5c06\u53d8\u91cf\u7f6e\u4e8e\u5927\u62ec\u53f7{}\u5185\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u53d8\u91cf\u66ff\u4ee3\u3002\u4f8b\u5982\uff0c<code>name = &quot;Alice&quot;; greeting = f&quot;Hello, {name}!&quot;<\/code>\u3002<strong>f\u5b57\u7b26\u4e32\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u652f\u6301\u8868\u8fbe\u5f0f\u6c42\u503c<\/strong>\uff0c\u5982<code>f&quot;The sum is {a + b}&quot;<\/code>\u3002\u4e0b\u9762\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662fPython\u4e2d\u6700\u65e9\u4f7f\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u548c\u4f7f\u7528str.format()\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u8f83\u4e3a\u53e4\u8001\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u867d\u7136\u7b80\u5355\u4f46\u662f\u529f\u80fd\u6709\u9650\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>greeting = &quot;Hello, %s! You are %d years old.&quot; % (name, age)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>%s<\/code>\u548c<code>%d<\/code>\u5206\u522b\u7528\u4e8e\u66ff\u4ee3\u5b57\u7b26\u4e32\u548c\u6574\u6570\uff0c\u540e\u9762\u7528<code>%<\/code>\u8fde\u63a5\u66ff\u4ee3\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001str.format()<\/h4>\n<\/p>\n<p><p>str.format()\u662fPython 3\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>greeting = &quot;Hello, {}! You are {} years old.&quot;.format(name, age)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u53c2\u6570\u6765\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">greeting = &quot;Hello, {name}! You are {age} years old.&quot;.format(name=&quot;Alice&quot;, age=25)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\uff08\u4e5f\u79f0\u4e3a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u4e3a\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0af\u6216F\uff0c\u5e76\u5728\u5927\u62ec\u53f7\u5185\u76f4\u63a5\u5199\u53d8\u91cf\u540d\u6216\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u66ff\u4ee3\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>greeting = f&quot;Hello, {name}! You are {age} years old.&quot;<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8868\u8fbe\u5f0f\u6c42\u503c<\/h4>\n<\/p>\n<p><p>f\u5b57\u7b26\u4e32\u4e0d\u4ec5\u53ef\u4ee5\u66ff\u4ee3\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u5728\u5927\u62ec\u53f7\u5185\u8fdb\u884c\u8868\u8fbe\u5f0f\u6c42\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 3<\/p>\n<p>result = f&quot;The sum of {a} and {b} is {a + b}.&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6a21\u677f\u5b57\u7b26\u4e32\uff08Template Strings\uff09\u662fPython\u4e2d\u53e6\u4e00\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u66f4\u5b89\u5168\u7684\u66ff\u4ee3\u53d8\u91cf\u65b9\u5f0f\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002\u6a21\u677f\u5b57\u7b26\u4e32\u5728<code>string<\/code>\u6a21\u5757\u4e2d\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>template = Template(&quot;Hello, $name! You are $age years old.&quot;)<\/p>\n<p>greeting = template.substitute(name=name, age=age)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u907f\u514dKeyError\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code><a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_substitute()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u907f\u514dKeyError\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">template = Template(&quot;Hello, $name! You are $age years old.&quot;)<\/p>\n<p>greeting = template.safe_substitute(name=&quot;Alice&quot;)<\/p>\n<p>print(greeting)  # Missing age will not r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se an error<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff09\u4e0d\u4ec5\u7528\u4e8e\u5b57\u7b26\u4e32\u5339\u914d\u548c\u641c\u7d22\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u66ff\u4ee3\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>template = &quot;Hello, {name}! You are {age} years old.&quot;<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>pattern = re.compile(r&#39;{(name|age)}&#39;)<\/p>\n<p>greeting = pattern.sub(lambda match: str(eval(match.group(1))), template)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u66ff\u4ee3<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u4ee3\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u66ff\u4ee3\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_variables(match):<\/p>\n<p>    variable = match.group(1)<\/p>\n<p>    if variable == &quot;name&quot;:<\/p>\n<p>        return &quot;Alice&quot;<\/p>\n<p>    elif variable == &quot;age&quot;:<\/p>\n<p>        return str(25)<\/p>\n<p>    return match.group(0)<\/p>\n<p>template = &quot;Hello, {name}! You are {age} years old.&quot;<\/p>\n<p>pattern = re.compile(r&#39;{(name|age)}&#39;)<\/p>\n<p>greeting = pattern.sub(replace_variables, template)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0cPython3\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u66ff\u4ee3\u53d8\u91cf\u7684\u9700\u6c42\u3002\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u66ff\u4ee3\u53d8\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<strong>\u7279\u522b\u63a8\u8350f\u5b57\u7b26\u4e32<\/strong>\uff0c\u5b83\u4e0d\u4ec5\u7b80\u6d01\u76f4\u89c2\uff0c\u8fd8\u652f\u6301\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u6c42\u503c\uff0c\u662f\u73b0\u4ee3Python\u4ee3\u7801\u7684\u9996\u9009\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662f\u66ff\u4ee3\u53d8\u91cf\uff0c\u4e3a\u4ec0\u4e48\u5728Python3\u4e2d\u4f7f\u7528\u5b83\u4eec\uff1f<\/strong><br \/>\u66ff\u4ee3\u53d8\u91cf\u662f\u6307\u5728\u7a0b\u5e8f\u4e2d\u7528\u6765\u4ee3\u8868\u5176\u4ed6\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\u7684\u53d8\u91cf\u3002\u5728Python3\u4e2d\uff0c\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5b83\u4eec\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u591a\u6b21\u4f7f\u7528\u67d0\u4e2a\u503c\u6216\u8868\u8fbe\u5f0f\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python3\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u6765\u5b9a\u4e49\u66ff\u4ee3\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u590d\u6742\u7684\u8ba1\u7b97\u7ed3\u679c\u8d4b\u7ed9\u4e00\u4e2a\u66ff\u4ee3\u53d8\u91cf\uff0c\u540e\u7eed\u7684\u4ee3\u7801\u4e2d\u53ea\u9700\u5f15\u7528\u8be5\u66ff\u4ee3\u53d8\u91cf\u5373\u53ef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">x = 10\ny = x * 2\nresult = y + 5  # \u4f7f\u7528\u66ff\u4ee3\u53d8\u91cfy\nprint(result)  # \u8f93\u51fa25\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>y<\/code> \u4f5c\u4e3a\u66ff\u4ee3\u53d8\u91cf\u4f7f\u5f97\u540e\u7eed\u7684\u8ba1\u7b97\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<p><strong>\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\u65f6\uff0c\u8981\u786e\u4fdd\u5b83\u4eec\u7684\u547d\u540d\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u7b80\u5355\u6216\u542b\u7cca\u7684\u540d\u79f0\uff08\u4f8b\u5982 <code>temp<\/code> \u6216 <code>var<\/code>\uff09\uff0c\u5e94\u9009\u62e9\u80fd\u591f\u6e05\u6670\u4f20\u8fbe\u8be5\u53d8\u91cf\u7528\u9014\u7684\u540d\u79f0\u3002\u6b64\u5916\uff0c\u5408\u7406\u63a7\u5236\u66ff\u4ee3\u53d8\u91cf\u7684\u6570\u91cf\uff0c\u8fc7\u591a\u7684\u66ff\u4ee3\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u6df7\u4e71\u3002\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\uff0c\u5e76\u786e\u4fdd\u6bcf\u4e2a\u66ff\u4ee3\u53d8\u91cf\u90fd\u6709\u660e\u786e\u7684\u4f5c\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\uff0c\u4f7f\u7528\u66ff\u4ee3\u53d8\u91cf\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f\u5b57\u7b26\u4e32\u3001\u6a21\u677f\u5b57\u7b26\u4e32\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5176\u4e2d\uff0cf\u5b57\u7b26\u4e32\u662f [&hellip;]","protected":false},"author":3,"featured_media":1072407,"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\/1072403"}],"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=1072403"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072403\/revisions"}],"predecessor-version":[{"id":1072410,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072403\/revisions\/1072410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072407"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}