{"id":1013401,"date":"2024-12-27T11:47:35","date_gmt":"2024-12-27T03:47:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013401.html"},"modified":"2024-12-27T11:47:37","modified_gmt":"2024-12-27T03:47:37","slug":"python%e4%b8%adstr%e5%a6%82%e4%bd%95%e7%9b%b8%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1013401.html","title":{"rendered":"python\u4e2dstr\u5982\u4f55\u76f8\u52a0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090835\/1dcea458-73f4-4067-aedf-928a7e63a496.webp\" alt=\"python\u4e2dstr\u5982\u4f55\u76f8\u52a0\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\uff08str\uff09\u76f8\u52a0\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u76f4\u63a5\u5b9e\u73b0\u3001\u901a\u8fc7join\u65b9\u6cd5\u6765\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u6216\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\uff08%\uff09\u6765\u7ec4\u5408\u5b57\u7b26\u4e32<\/strong>\u3002\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5404\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5176\u4e2d\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u52a0\u53f7\u8fd0\u7b97\u7b26\uff0cPython\u4f1a\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5728\u539f\u6709\u5b57\u7b26\u4e32\u7684\u57fa\u7840\u4e0a\u4fee\u6539\uff0c\u56e0\u6b64\u5bf9\u4e8e\u957f\u5b57\u7b26\u4e32\u548c\u5927\u91cf\u62fc\u63a5\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u662fPython\u4e2d\u6700\u76f4\u89c2\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u65b9\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5904\u7406\u5c11\u91cf\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c\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)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u52a0\u53f7\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32 <code>str1<\/code> \u548c <code>str2<\/code> \u4ee5\u53ca\u4e00\u4e2a\u7a7a\u683c\u5b57\u7b26\u4e32 <code>&quot; &quot;<\/code> \u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32 <code>result<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528join\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>join<\/code> \u65b9\u6cd5\u662fPython\u4e2d\u7528\u4e8e\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u53e6\u4e00\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u5408\u5728\u9700\u8981\u8fde\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u76f8\u6bd4\u4e8e\u52a0\u53f7\u8fd0\u7b97\u7b26\uff0c<code>join<\/code> \u65b9\u6cd5\u6027\u80fd\u66f4\u597d\uff0c\u56e0\u4e3a\u5b83\u662f\u901a\u8fc7\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\uff09\u5e76\u5c06\u5176\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&quot;Hello&quot;, &quot;World&quot;]<\/p>\n<p>result = &quot; &quot;.join(words)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5217\u8868 <code>words<\/code> \u4f20\u9012\u7ed9 <code>join<\/code> \u65b9\u6cd5\uff0c\u5e76\u6307\u5b9a\u4e00\u4e2a\u7a7a\u683c\u5b57\u7b26\u4e32 <code>&quot; &quot;<\/code> \u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4ece\u800c\u5c06\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09<\/p>\n<\/p>\n<p><p>\u4ecePython 3.6\u5f00\u59cb\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a\u5b57\u6bcd <code>f<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\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 = f&quot;{str1} {str2}&quot;<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32 <code>f&quot;{str1} {str2}&quot;<\/code>\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u76f4\u63a5\u5d4c\u5165\u4e86\u53d8\u91cf <code>str1<\/code> \u548c <code>str2<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\uff08%\uff09<\/p>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\uff08%\uff09\u662fPython\u4e2d\u53e6\u4e00\u79cd\u5b57\u7b26\u4e32\u62fc\u63a5\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u683c\u5f0f\u5316\u5904\u7406\u7684\u573a\u666f\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;%s %s&quot; % (str1, str2)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26 <code>%<\/code>\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684 <code>%s<\/code> \u5360\u4f4d\u7b26\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528format\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>format<\/code> \u65b9\u6cd5\u662fPython\u4e2d\u53e6\u4e00\u79cd\u7528\u4e8e\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u3002\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u8c03\u7528 <code>format<\/code> \u65b9\u6cd5\u4f20\u9012\u53c2\u6570\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 = &quot;{} {}&quot;.format(str1, str2)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>format<\/code> \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684 <code>{}<\/code> \u5360\u4f4d\u7b26\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5b57\u7b26\u4e32\u76f8\u52a0\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u867d\u7136\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u5176\u6027\u80fd\u53ef\u80fd\u4e0d\u5982 <code>join<\/code> \u65b9\u6cd5\u3002<code>join<\/code> \u65b9\u6cd5\u901a\u8fc7\u4e00\u6b21\u6027\u5206\u914d\u5185\u5b58\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5728\u9700\u8981\u8fde\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u5e38\u6bd4\u52a0\u53f7\u8fd0\u7b97\u7b26\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09\u548c <code>format<\/code> \u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u76f8\u5f53\uff0c\u4e14\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\u3002\u5bf9\u4e8e\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u4ee3\u7801\u7b80\u5355\u76f4\u89c2\u3002<\/li>\n<li><strong>join\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u6027\u80fd\u66f4\u4f18\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08f-string\uff09<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u573a\u666f\uff0c\u8bed\u6cd5\u7b80\u6d01\uff0c\u6613\u8bfb\u6027\u5f3a\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5316\u64cd\u4f5c\u7b26\uff08%\uff09\u548cformat\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u683c\u5f0f\u6709\u7279\u6b8a\u8981\u6c42\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u8fd9\u4e9b\u5b57\u7b26\u4e32\u76f8\u52a0\u7684\u65b9\u6cd5\u548c\u5b83\u4eec\u5404\u81ea\u7684\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u9700\u6c42\u4e2d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08<code>+<\/code>\uff09\u8fd0\u7b97\u7b26\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u53ea\u9700\u5c06\u9700\u8981\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u7528\u52a0\u53f7\u8fde\u63a5\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<code>result = &quot;Hello, &quot; + &quot;world!&quot;<\/code>\uff0c\u6b64\u65f6<code>result<\/code>\u7684\u503c\u5c06\u662f<code>&quot;Hello, world!&quot;<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528join\u65b9\u6cd5\u4e0e\u52a0\u53f7\u8fde\u63a5\u5b57\u7b26\u4e32\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u65f6\u63d2\u5165\u5206\u9694\u7b26\uff0c\u8fd9\u5bf9\u4e8e\u5904\u7406\u591a\u4e2a\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u65b9\u4fbf\u3002\u4f8b\u5982\uff0c<code>&quot;, &quot;.join([&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;])<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u9017\u53f7\u548c\u7a7a\u683c\u7684\u5b57\u7b26\u4e32<code>&quot;apple, banana, cherry&quot;<\/code>\u3002\u800c\u4f7f\u7528\u52a0\u53f7\u8fde\u63a5\u5b57\u7b26\u4e32\u65f6\uff0c\u5219\u65e0\u6cd5\u76f4\u63a5\u6307\u5b9a\u5206\u9694\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u76f8\u52a0\u4f1a\u5bfc\u81f4\u7c7b\u578b\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;The number is &quot;<\/code>\u548c\u4e00\u4e2a\u6570\u5b57<code>5<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u8fde\u63a5\uff1a<code>result = &quot;The number is &quot; + str(5)<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>&quot;The number is 5&quot;<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\uff08str\uff09\u76f8\u52a0\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u76f4\u63a5\u5b9e\u73b0\u3001\u901a\u8fc7join\u65b9\u6cd5\u6765\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1013408,"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\/1013401"}],"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=1013401"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013401\/revisions"}],"predecessor-version":[{"id":1013410,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013401\/revisions\/1013410"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013408"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}