{"id":1146540,"date":"2025-01-08T23:17:45","date_gmt":"2025-01-08T15:17:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146540.html"},"modified":"2025-01-08T23:17:49","modified_gmt":"2025-01-08T15:17:49","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e4%bc%a0%e5%85%a5%e4%b8%a4%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146540.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u5982\u4f55\u4f20\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182545\/fdb4b8e6-38a5-4951-a081-29c34457559c.webp\" alt=\"python\u5b57\u7b26\u4e32\u5982\u4f55\u4f20\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u5b57\u7b26\u4e32\u5982\u4f55\u4f20\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32<\/strong>\uff1aPython\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u4f20\u5165\u5e76\u8fdb\u884c\u64cd\u4f5c\uff0c<strong>\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001f-strings\u3001join\u65b9\u6cd5<\/strong>\u7b49\u90fd\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u4e3b\u8981\u4ecb\u7ecd<strong>f-strings<\/strong>\u8fd9\u4e00\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u76f4\u89c2\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>f-strings<\/strong>\u662f\u4e00\u79cd\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u5f15\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd<code>f<\/code>\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u5185\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5d4c\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u3002\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684<code>%<\/code>\u683c\u5f0f\u5316\u548c<code>str.format<\/code>\u65b9\u6cd5\uff0c<strong>f-strings<\/strong>\u4e0d\u4ec5\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>result = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>f-strings<\/strong>\u4e0d\u4ec5\u53ef\u4ee5\u5d4c\u5165\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5d4c\u5165\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 10<\/p>\n<p>result = f&quot;Double value: {value * 2}&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Double value: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7<code>+<\/code>\u64cd\u4f5c\u7b26\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;World&quot;<\/p>\n<p>result = str1 + &quot; &quot; + str2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65f6\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5\u5305\u62ec<code>%<\/code>\u64cd\u4f5c\u7b26\u548c<code>str.format<\/code>\u65b9\u6cd5\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u65b9\u6cd5\u8f83\u4e3a\u53e4\u8001\uff0c\u4f46\u4ecd\u7136\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>result = &quot;Name: %s, Age: %d&quot; % (name, age)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>str.format<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>result = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u4e9b\u65b9\u6cd5\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u5b83\u4eec\u7684\u8bed\u6cd5\u76f8\u5bf9\u8f83\u4e3a\u5197\u957f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001f-strings<\/h3>\n<\/p>\n<p><p>f-strings\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd<code>f<\/code>\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u5185\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5d4c\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>result = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-strings\u4e0d\u4ec5\u53ef\u4ee5\u5d4c\u5165\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u5d4c\u5165\u590d\u6742\u7684\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 10<\/p>\n<p>result = f&quot;Double value: {value * 2}&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Double value: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-strings\u7684\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff0c\u6027\u80fd\u4e5f\u4f18\u4e8e\u4f20\u7edf\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u56e0\u6b64\u5728\u73b0\u4ee3Python\u4ee3\u7801\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001join\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>join<\/code>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u5b83\u9700\u8981\u4e00\u4e2a\u8fed\u4ee3\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u63d2\u5165\u6307\u5b9a\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;World&quot;<\/p>\n<p>result = &quot; &quot;.join([str1, str2])<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>join<\/code>\u65b9\u6cd5\u5728\u9700\u8981\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u5217\u8868\u6216\u5176\u4ed6\u8fed\u4ee3\u5668\u4e2d\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u6bcf\u79cd\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\uff0c\u4f46\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65f6\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p><strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u65b9\u6cd5\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u4f46\u8bed\u6cd5\u76f8\u5bf9\u8f83\u4e3a\u5197\u957f\u3002<\/p>\n<\/p>\n<p><p><strong>f-strings<\/strong>\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff0c\u6027\u80fd\u4f18\u4e8e\u4f20\u7edf\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u73b0\u4ee3Python\u4ee3\u7801\u4e2d\u7684\u5404\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>join\u65b9\u6cd5<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5f53\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u5217\u8868\u6216\u5176\u4ed6\u8fed\u4ee3\u5668\u4e2d\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u65b9\u9762\uff0cf-strings\u7684\u8868\u73b0\u4f18\u4e8e\u5176\u4ed6\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u5bf9\u6bd4\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>setup = &quot;&quot;&quot;<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>age = 30<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>code1 = &quot;&quot;&quot;<\/p>\n<p>result = &quot;Name: %s, Age: %d&quot; % (name, age)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>code2 = &quot;&quot;&quot;<\/p>\n<p>result = &quot;Name: {}, Age: {}&quot;.format(name, age)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>code3 = &quot;&quot;&quot;<\/p>\n<p>result = f&quot;Name: {name}, Age: {age}&quot;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>time1 = timeit.timeit(code1, setup=setup, number=1000000)<\/p>\n<p>time2 = timeit.timeit(code2, setup=setup, number=1000000)<\/p>\n<p>time3 = timeit.timeit(code3, setup=setup, number=1000000)<\/p>\n<p>print(f&quot;Percentage method: {time1:.6f} seconds&quot;)<\/p>\n<p>print(f&quot;str.format method: {time2:.6f} seconds&quot;)<\/p>\n<p>print(f&quot;f-strings method: {time3:.6f} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Percentage method: 0.726432 seconds<\/p>\n<p>str.format method: 0.893216 seconds<\/p>\n<p>f-strings method: 0.525832 seconds<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cf-strings\u5728\u6027\u80fd\u4e0a\u5177\u6709\u660e\u663e\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\u65f6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u548c\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7<code>+<\/code>\u64cd\u4f5c\u7b26\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u9700\u6c42\uff0c\u5efa\u8bae\u4f7f\u7528f-strings\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u591a\u6b21\u62fc\u63a5<\/strong>\uff1a\u5728\u5faa\u73af\u4e2d\u907f\u514d\u591a\u6b21\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u6bcf\u6b21\u62fc\u63a5\u90fd\u4f1a\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u6216\u5176\u4ed6\u9ad8\u6548\u7684\u62fc\u63a5\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8003\u8651\u53ef\u8bfb\u6027<\/strong>\uff1a\u5728\u9009\u62e9\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\u65f6\uff0c\u9664\u4e86\u6027\u80fd\uff0c\u8fd8\u5e94\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002f-strings\u7684\u8bed\u6cd5\u7b80\u6d01\u76f4\u89c2\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Python\u7248\u672c\u517c\u5bb9\u6027<\/strong>\uff1a\u5728\u4f7f\u7528f-strings\u65f6\uff0c\u786e\u4fdd\u4ee3\u7801\u8fd0\u884c\u7684Python\u7248\u672c\u662f3.6\u53ca\u4ee5\u4e0a\u3002\u5982\u679c\u9700\u8981\u517c\u5bb9\u66f4\u65e9\u7248\u672c\u7684Python\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>str.format<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u6ce8\u610f\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u7b49\u5b89\u5168\u95ee\u9898\u3002\u5c3d\u91cf\u907f\u514d\u76f4\u63a5\u5c06\u7528\u6237\u8f93\u5165\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u4f7f\u7528\u5b89\u5168\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001f-strings\u548cjoin\u65b9\u6cd5\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>f-strings\u4f5c\u4e3aPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u7684\u4e00\u79cd\u65b0\u65b9\u6cd5\uff0c\u4ee5\u5176\u7b80\u6d01\u76f4\u89c2\u7684\u8bed\u6cd5\u548c\u4f18\u8d8a\u7684\u6027\u80fd\uff0c\u9010\u6e10\u6210\u4e3a\u73b0\u4ee3Python\u4ee3\u7801\u4e2d\u7684\u9996\u9009\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u9ad8\u8d28\u91cf\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5408\u5e76\u4e24\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u5b57\u7b26\u4e32\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\uff0c\u4f8b\u5982\uff1a<code>string1 + string2<\/code>\u3002\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>f-strings<\/code>\u6216\u8005<code>format()<\/code>\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<code>f&quot;{string1}{string2}&quot;<\/code>\u6216<code>&quot;{}{}&quot;.format(string1, string2)<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u62fc\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u52a0\u53f7\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u8fdb\u884c\u62fc\u63a5\uff0c\u5982\uff1a<code>&#39;&#39;.join([string1, string2, string3])<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff1f<\/strong><br \/>\u5728\u62fc\u63a5\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u7a7a\u683c\u3002\u4f8b\u5982\uff1a<code>string1 + &quot; &quot; + string2<\/code>\u3002\u5982\u679c\u5e0c\u671b\u5728\u62fc\u63a5\u65f6\u52a8\u6001\u6dfb\u52a0\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5e76\u5728\u5b57\u7b26\u4e32\u4e2d\u8bbe\u7f6e\u7a7a\u683c\uff0c\u5982\uff1a<code>&#39; &#39;.join([string1, string2])<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u62fc\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u5728\u6bcf\u4e2a\u90e8\u5206\u4e4b\u95f4\u90fd\u6709\u7a7a\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u7b26\u4e32\u5982\u4f55\u4f20\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32\uff1aPython\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u4f20\u5165\u5e76\u8fdb\u884c\u64cd\u4f5c\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c [&hellip;]","protected":false},"author":3,"featured_media":1146551,"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\/1146540"}],"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=1146540"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146540\/revisions"}],"predecessor-version":[{"id":1146557,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146540\/revisions\/1146557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146551"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}