{"id":1123976,"date":"2025-01-08T19:37:21","date_gmt":"2025-01-08T11:37:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1123976.html"},"modified":"2025-01-08T19:37:24","modified_gmt":"2025-01-08T11:37:24","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8b%bc%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e9%87%8f%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1123976.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085226\/469164bd-0f47-4ef0-abd9-599a32aeafc2.webp\" alt=\"python\u4e2d\u5982\u4f55\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-strings\u3001\u4ee5\u53ca\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26+\u7b49\u65b9\u6cd5\u3002\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528f-strings\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u53ef\u8bfb\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7<code>%<\/code>\u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code>\u65b9\u6cd5\u4ee5\u53ca\u6700\u65b0\u7684f-strings\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1.1 <code>%<\/code>\u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/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\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6ee1\u8db3\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u9700\u6c42\uff0c\u4f46\u76f8\u5bf9\u8f83\u8001\u65e7\uff0c\u4e0d\u63a8\u8350\u5728\u73b0\u4ee3\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1.2 <code>str.format()<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/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><code>str.format()<\/code>\u65b9\u6cd5\u6bd4<code>%<\/code>\u64cd\u4f5c\u7b26\u66f4\u7075\u6d3b\u4e14\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u4f46\u5728Python 3.6\u4e4b\u540e\uff0cf-strings\u7684\u5f15\u5165\u4f7f\u5f97\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u53d8\u5f97\u4e0d\u90a3\u4e48\u6d41\u884c\u4e86\u3002<\/p>\n<\/p>\n<p><h4>1.3 f-strings<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/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><p><strong>f-strings\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u5728Python 3.6\u4e2d\u5f15\u5165\uff0c\u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u8fd8\u652f\u6301\u5185\u5d4c\u7684\u8868\u8fbe\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>+<\/code>\u64cd\u4f5c\u7b26\u53ef\u4ee5\u76f4\u63a5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">part1 = &quot;Hello, &quot;<\/p>\n<p>part2 = &quot;World&quot;<\/p>\n<p>greeting = part1 + part2<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528join\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u7531\u5217\u8868\u6216\u5143\u7ec4\u7b49\u96c6\u5408\u7c7b\u578b\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&quot;Hello&quot;, &quot;world&quot;, &quot;from&quot;, &quot;Python&quot;]<\/p>\n<p>greeting = &quot; &quot;.join(words)<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>locals()<\/code>\u548c<code>globals()<\/code><\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u52a8\u6001\u751f\u6210\u53d8\u91cf\u540d\u5e76\u62fc\u63a5\u5176\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>locals()<\/code>\u548c<code>globals()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">var1 = &quot;Hello&quot;<\/p>\n<p>var2 = &quot;World&quot;<\/p>\n<p>var_name = &quot;var&quot;<\/p>\n<p>greeting = locals()[f&quot;{var_name}1&quot;] + &quot; &quot; + locals()[f&quot;{var_name}2&quot;]<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6765\u52a8\u6001\u7ba1\u7406\u548c\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">variables = {<\/p>\n<p>    &quot;name1&quot;: &quot;Hello&quot;,<\/p>\n<p>    &quot;name2&quot;: &quot;World&quot;<\/p>\n<p>}<\/p>\n<p>greeting = variables[&quot;name1&quot;] + &quot; &quot; + variables[&quot;name2&quot;]<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>6.1 \u6570\u636e\u5904\u7406\u548c\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u3002f-strings\u7684\u5185\u5d4c\u8868\u8fbe\u5f0f\u529f\u80fd\u4f7f\u5f97\u5b83\u5728\u8fd9\u4e00\u573a\u666f\u4e2d\u5c24\u4e3a\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u6765\u751f\u6210\u65e5\u5fd7\u4fe1\u606f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002f-strings\u4e0d\u4ec5\u652f\u6301\u8868\u8fbe\u5f0f\uff0c\u8fd8\u80fd\u901a\u8fc7\u5d4c\u5165\u590d\u6742\u7684\u903b\u8f91\u6765\u5b9e\u73b0\u52a8\u6001\u65e5\u5fd7\u4fe1\u606f\u751f\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>name = &quot;John&quot;<\/p>\n<p>age = 30<\/p>\n<p>logging.info(f&quot;User {name} has logged in. Age: {age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 Web\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u7528\u4e8e\u751f\u6210HTML\u3001SQL\u67e5\u8be2\u7b49\u3002f-strings\u7684\u7b80\u6d01\u6027\u548c\u9ad8\u6548\u6027\u4f7f\u5f97\u5b83\u5728\u8fd9\u4e00\u9886\u57df\u4e5f\u5f97\u5230\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">username = &quot;john_doe&quot;<\/p>\n<p>query = f&quot;SELECT * FROM users WHERE username=&#39;{username}&#39;&quot;<\/p>\n<p>print(query)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u6709\u5dee\u5f02\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cf-strings \u548c <code>str.join()<\/code> \u65b9\u6cd5\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>f-strings<\/strong><\/h2>\n<p>f_string_time = timeit.timeit(&#39;f&quot;Hello, {name}.&quot; + &quot; You are {age} years old.&quot;&#39;, globals=globals())<\/p>\n<h2><strong>str.join()<\/strong><\/h2>\n<p>join_time = timeit.timeit(&#39;&quot; &quot;.join([&quot;Hello,&quot;, name + &quot;.&quot;, &quot;You are&quot;, str(age), &quot;years old.&quot;])&#39;, globals=globals())<\/p>\n<p>print(f&quot;f-strings time: {f_string_time}&quot;)<\/p>\n<p>print(f&quot;join time: {join_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\u3002\u4ece\u4f20\u7edf\u7684<code>%<\/code>\u64cd\u4f5c\u7b26\uff0c\u5230<code>str.format()<\/code>\uff0c\u518d\u5230\u73b0\u4ee3\u7684f-strings\uff0c\u6bcf\u4e00\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>f-strings\u7531\u4e8e\u5176\u7b80\u6d01\u3001\u9ad8\u6548\u3001\u53ef\u8bfb\u6027\u5f3a\u7b49\u4f18\u70b9\uff0c\u662f\u76ee\u524d\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u3002<\/strong>\u5176\u4ed6\u65b9\u6cd5\u5982<code>+<\/code>\u64cd\u4f5c\u7b26\u3001<code>str.join()<\/code>\u7b49\u4e5f\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u52a8\u6001\u521b\u5efa\u53d8\u91cf\u540d\u4ee5\u62fc\u63a5\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u901a\u5e38\u4e0d\u5efa\u8bae\u52a8\u6001\u521b\u5efa\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u4ee3\u7801\u53ef\u8bfb\u6027\u5dee\u548c\u7ef4\u62a4\u56f0\u96be\u3002\u7136\u800c\uff0c\u5982\u679c\u786e\u5b9e\u9700\u8981\u62fc\u63a5\u5b57\u7b26\u4e32\u4ee5\u5f62\u6210\u53d8\u91cf\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u6765\u5b58\u50a8\u8fd9\u4e9b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5b57\u5178\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\uff0c\u5c06\u5bf9\u5e94\u7684\u503c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u62fc\u63a5\u5b57\u7b26\u4e32\u6765\u8bbf\u95ee\u76f8\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u65b9\u6cd5\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u3001join()\u65b9\u6cd5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u7b49\u3002\u4f7f\u7528\u52a0\u53f7\u8fdb\u884c\u62fc\u63a5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\u3002join()\u65b9\u6cd5\u5728\u5904\u7406\u591a\u4e2a\u5b57\u7b26\u4e32\u65f6\u66f4\u52a0\u9ad8\u6548\uff0c\u800cf-string\u5219\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\uff0c\u5c24\u5176\u662f\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u907f\u514d\u5b57\u7b26\u4e32\u62fc\u63a5\u5f15\u8d77\u7684\u6f5c\u5728\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u786e\u4fdd\u62fc\u63a5\u7684\u5bf9\u8c61\u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u907f\u514d\u51fa\u73b0\u7c7b\u578b\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684str()\u51fd\u6570\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5e76\u907f\u514d\u62fc\u63a5\u65f6\u7684\u62fc\u5199\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528f-string\u6216.format()\u65b9\u6cd5\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u8868\u8fbe\u610f\u56fe\uff0c\u4ece\u800c\u964d\u4f4e\u51fa\u9519\u7684\u6982\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\u53d8\u91cf\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-strings\u3001\u4ee5\u53ca\u5b57\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":1123987,"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\/1123976"}],"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=1123976"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123976\/revisions"}],"predecessor-version":[{"id":1123991,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123976\/revisions\/1123991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1123987"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1123976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1123976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1123976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}