{"id":1036333,"date":"2024-12-31T12:06:09","date_gmt":"2024-12-31T04:06:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036333.html"},"modified":"2024-12-31T12:06:12","modified_gmt":"2024-12-31T04:06:12","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e4%b8%80%e4%b8%b2%e5%ad%97%e7%ac%a6%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036333.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u4e00\u4e32\u5b57\u7b26\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/57a02819-9874-44ea-80d8-5d4b8f1add35.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7ed9\u4e00\u4e32\u5b57\u7b26\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u8d4b\u503c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u5b57\u7b26\u4e32\u66ff\u6362\u7b49\u65b9\u5f0f\u7ed9\u4e00\u4e32\u5b57\u7b26\u8d4b\u503c<\/strong>\u3002\u5176\u4e2d\uff0c\u53d8\u91cf\u8d4b\u503c\u662f\u6700\u57fa\u7840\u4e14\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<strong>\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\u5c06\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf<\/strong>\uff0c\u7136\u540e\u5bf9\u8fd9\u4e2a\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u3002<strong>\u5b57\u7b26\u4e32\u8fde\u63a5\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u6216join\u65b9\u6cd5<\/strong>\uff0c\u800c\u5b57\u7b26\u4e32\u66ff\u6362\u5219\u53ef\u4ee5\u4f7f\u7528replace\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u53d8\u91cf\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u53d8\u91cf\u8d4b\u503c\u662fPython\u4e2d\u6700\u57fa\u7840\u4e14\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\u5c06\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u53d8\u91cf\u5e76\u8d4b\u503c\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u884c\u4ee3\u7801\u4e2d\uff0c<code>my_string<\/code> \u662f\u53d8\u91cf\u540d\uff0c<code>&quot;Hello, World!&quot;<\/code> \u662f\u8d4b\u503c\u7ed9\u8fd9\u4e2a\u53d8\u91cf\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbf\u95ee\u548c\u64cd\u4f5c\u53d8\u91cf\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">print(my_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7 <code>print<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4fee\u6539\u53d8\u91cf\u7684\u503c\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, Python!&quot;<\/p>\n<p>print(my_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u91cd\u65b0\u7ed9\u53d8\u91cf\u8d4b\u503c\uff0c\u4ece\u800c\u4fee\u6539\u53d8\u91cf\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u6216 <code>join<\/code> \u65b9\u6cd5\u6765\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;, &quot;<\/p>\n<p>str3 = &quot;World&quot;<\/p>\n<p>result = str1 + str2 + str3<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello, World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u52a0\u53f7\u5c06 <code>str1<\/code>\u3001<code>str2<\/code> \u548c <code>str3<\/code> \u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>result<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528 <code>join<\/code> \u65b9\u6cd5\u8fde\u63a5\u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">str_list = [&quot;Hello&quot;, &quot;, &quot;, &quot;World&quot;]<\/p>\n<p>result = &quot;&quot;.join(str_list)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello, World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>join<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868 <code>str_list<\/code> \u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>result<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u7b26\u4e32\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>replace<\/code> \u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>replace<\/code> \u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>new_string = original_string.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>replace<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u4e2d\u7684 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Python&quot;\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>new_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u66ff\u6362\u591a\u4e2a\u5b50\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World! Welcome to the World of Python.&quot;<\/p>\n<p>new_string = original_string.replace(&quot;World&quot;, &quot;Universe&quot;)<\/p>\n<p>print(new_string)  # \u8f93\u51fa: Hello, Universe! Welcome to the Universe of Python.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>replace<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u4e2d\u7684\u6240\u6709 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Universe&quot;\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>new_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u7ed9\u5b57\u7b26\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>format<\/code> \u65b9\u6cd5\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">name = &quot;Python&quot;<\/p>\n<p>formatted_string = &quot;Hello, {}!&quot;.format(name)<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>format<\/code> \u65b9\u6cd5\u5c06 <code>name<\/code> \u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>formatted_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528 f-string \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">name = &quot;Python&quot;<\/p>\n<p>formatted_string = f&quot;Hello, {name}!&quot;<\/p>\n<p>print(formatted_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 f-string \u5c06 <code>name<\/code> \u53d8\u91cf\u7684\u503c\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>formatted_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7b26\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\uff0c\u5e76\u8d4b\u503c\u7ed9\u65b0\u7684\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>sliced_string = original_string[7:12]<\/p>\n<p>print(sliced_string)  # \u8f93\u51fa: World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u7b26 <code>[7:12]<\/code> \u83b7\u53d6 <code>original_string<\/code> \u4e2d\u7684\u90e8\u5206\u5b50\u4e32\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>sliced_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ec4\u5408\u5207\u7247\u548c\u8fde\u63a5\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>part1 = original_string[:7]<\/p>\n<p>part2 = original_string[7:12]<\/p>\n<p>part3 = original_string[12:]<\/p>\n<p>new_string = part1 + part2 + part3<\/p>\n<p>print(new_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u5c06 <code>original_string<\/code> \u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u901a\u8fc7\u52a0\u53f7\u5c06\u8fd9\u4e9b\u90e8\u5206\u91cd\u65b0\u8fde\u63a5\u8d77\u6765\uff0c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>new_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5b57\u7b26\u4e32\u5e38\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u8fdb\u884c\u8d4b\u503c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u8d4b\u503c\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">my_string = &quot;Hello, World!&quot;<\/p>\n<p>print(my_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u5b57\u7b26\u4e32\u5e38\u91cf <code>&quot;Hello, World!&quot;<\/code> \u7ed9\u53d8\u91cf <code>my_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ec4\u5408\u5b57\u7b26\u4e32\u5e38\u91cf\u548c\u53d8\u91cf\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">name = &quot;Python&quot;<\/p>\n<p>greeting = &quot;Hello, &quot; + name + &quot;!&quot;<\/p>\n<p>print(greeting)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u52a0\u53f7\u5c06\u5b57\u7b26\u4e32\u5e38\u91cf <code>&quot;Hello, &quot;<\/code> \u548c\u53d8\u91cf <code>name<\/code> \u4ee5\u53ca\u5b57\u7b26\u4e32\u5e38\u91cf <code>&quot;!&quot;<\/code> \u8fde\u63a5\u8d77\u6765\uff0c\u8d4b\u503c\u7ed9\u53d8\u91cf <code>greeting<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong><code>upper<\/code> \u548c <code>lower<\/code> \u65b9\u6cd5\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>upper_string = original_string.upper()<\/p>\n<p>lower_string = original_string.lower()<\/p>\n<p>print(upper_string)  # \u8f93\u51fa: HELLO, WORLD!<\/p>\n<p>print(lower_string)  # \u8f93\u51fa: hello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>upper<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>upper_string<\/code>\uff1b\u901a\u8fc7 <code>lower<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>lower_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong><code>strip<\/code> \u65b9\u6cd5\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;   Hello, World!   &quot;<\/p>\n<p>stripped_string = original_string.strip()<\/p>\n<p>print(stripped_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>strip<\/code> \u65b9\u6cd5\u53bb\u9664 <code>original_string<\/code> \u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>stripped_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong><code>split<\/code> \u548c <code>join<\/code> \u65b9\u6cd5\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>split_list = original_string.split(&quot;, &quot;)<\/p>\n<p>joined_string = &quot;, &quot;.join(split_list)<\/p>\n<p>print(split_list)  # \u8f93\u51fa: [&#39;Hello&#39;, &#39;World!&#39;]<\/p>\n<p>print(joined_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>split<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u6309\u7167 &quot;, &quot; \u5206\u5272\u6210\u5217\u8868\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>split_list<\/code>\uff1b\u901a\u8fc7 <code>join<\/code> \u65b9\u6cd5\u5c06 <code>split_list<\/code> \u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>joined_string<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165 <code>re<\/code> \u6a21\u5757\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>import re<\/code> \u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528 <code>re.sub<\/code> \u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>new_string = re.sub(r&quot;World&quot;, &quot;Python&quot;, original_string)<\/p>\n<p>print(new_string)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>re.sub<\/code> \u65b9\u6cd5\u5c06 <code>original_string<\/code> \u4e2d\u7684 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Python&quot;\uff0c\u5e76\u8d4b\u503c\u7ed9\u53d8\u91cf <code>new_string<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528 <code>re.findall<\/code> \u65b9\u6cd5\u67e5\u627e\u5339\u914d\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World! Welcome to the World of Python.&quot;<\/p>\n<p>matches = re.findall(r&quot;World&quot;, original_string)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;World&#39;, &#39;World&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>re.findall<\/code> \u65b9\u6cd5\u67e5\u627e <code>original_string<\/code> \u4e2d\u6240\u6709\u5339\u914d &quot;World&quot; \u7684\u5b50\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868 <code>matches<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u8d4b\u503c\u548c\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684\u53d8\u91cf\u8d4b\u503c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\uff0c\u8fd8\u662f\u9ad8\u7ea7\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8d4b\u503c\u5b57\u7b26\u4e32\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u4e3a\u5b57\u7b26\u4e32\u53d8\u91cf\u8d4b\u503c\u3002\u4f60\u53ea\u9700\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\u5c06\u5b57\u7b26\u4e32\u8d4b\u7ed9\u53d8\u91cf\u540d\uff0c\u4f8b\u5982\uff1a<code>my_string = &quot;Hello, World!&quot;<\/code>\u3002\u8fd9\u6837\uff0c<code>my_string<\/code>\u5c31\u5305\u542b\u4e86&quot;Hello, World!&quot;\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\uff0c\u4e24\u8005\u6548\u679c\u76f8\u540c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u6765\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<code>greeting = &quot;Hello, &quot; + &quot;World!&quot;<\/code>\u3002\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32<code>greeting<\/code>\uff0c\u5176\u503c\u4e3a&quot;Hello, World!&quot;\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u9ad8\u6548\u5730\u8fde\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<code>&quot;,&quot;.join([&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;])<\/code>\u5c06\u8fd4\u56de&quot;apple,banana,cherry&quot;\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u3002\u5982\u679c\u9700\u8981\u6539\u53d8\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\u548c\u62fc\u63a5\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a<code>original_string = &quot;Hello&quot;<\/code>, <code>modified_string = original_string + &quot; World!&quot;<\/code>\u3002\u8fd9\u6837\uff0c<code>modified_string<\/code>\u5c06\u5305\u542b&quot;Hello World!&quot;\uff0c\u800c<code>original_string<\/code>\u4ecd\u7136\u4fdd\u6301\u4e3a&quot;Hello&quot;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u8d4b\u503c\u3001\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u5b57\u7b26\u4e32\u66ff\u6362\u7b49\u65b9\u5f0f\u7ed9\u4e00\u4e32\u5b57\u7b26\u8d4b\u503c\u3002\u5176\u4e2d\uff0c\u53d8\u91cf\u8d4b\u503c\u662f\u6700\u57fa\u7840\u4e14\u6700\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1036340,"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\/1036333"}],"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=1036333"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036333\/revisions"}],"predecessor-version":[{"id":1036341,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036333\/revisions\/1036341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036340"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}