{"id":1112982,"date":"2025-01-08T17:43:33","date_gmt":"2025-01-08T09:43:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112982.html"},"modified":"2025-01-08T17:43:35","modified_gmt":"2025-01-08T09:43:35","slug":"python%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%9a%84%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112982.html","title":{"rendered":"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074752\/09b33eaf-eb11-44b0-9824-20f8e213ae86.webp\" alt=\"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\" \/><\/p>\n<p><p> <strong>Python\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecreplace()\u65b9\u6cd5\u3001translate()\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong> \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528replace()\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u76f4\u89c2\u3002\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528translate()\u65b9\u6cd5\uff0c\u5b83\u5728\u9700\u8981\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u6700\u540e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\u6765\u8fdb\u884c\u590d\u6742\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u5bf9replace()\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>replace()\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u5185\u7f6e\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u5b83\u7684\u8bed\u6cd5\u4e3a\uff1astr.replace(old, new[, count])\uff0c\u5176\u4e2dold\u8868\u793a\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff0cnew\u8868\u793a\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32\uff0ccount\u662f\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u66ff\u6362\u7684\u6b21\u6570\uff08\u9ed8\u8ba4\u662f\u5168\u90e8\u66ff\u6362\uff09\u3002\u4f8b\u5982\uff0c&quot;hello world&quot;.replace(&quot;world&quot;, &quot;Python&quot;)\u5c06\u8fd4\u56de&quot;hello Python&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001replace()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>replace()\u65b9\u6cd5\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u5355\u4e14\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.replace(old, new[, count])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>old<\/code>\uff1a\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>new<\/code>\uff1a\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>count<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u66ff\u6362\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a\u5168\u90e8\u66ff\u6362\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello world, welcome to the world of Python.&quot;<\/p>\n<h2><strong>\u66ff\u6362\u6240\u6709\u7684 &quot;world&quot; \u4e3a &quot;universe&quot;<\/strong><\/h2>\n<p>new_text = text.replace(&quot;world&quot;, &quot;universe&quot;)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello universe, welcome to the universe of Python.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u201cworld\u201d\u66ff\u6362\u4e3a\u201cuniverse\u201d\u3002\u5982\u679c\u6211\u4eec\u53ea\u60f3\u66ff\u6362\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u201cworld\u201d\uff0c\u53ef\u4ee5\u4f20\u9012count\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_text = text.replace(&quot;world&quot;, &quot;universe&quot;, 1)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello universe, welcome to the world of Python.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001translate()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>translate()\u65b9\u6cd5\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6620\u5c04\u8868\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.translate(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>table<\/code>\u662f\u4e00\u4e2a\u6620\u5c04\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7<code>str.maketrans()<\/code>\u65b9\u6cd5\u521b\u5efa\u3002<code>str.maketrans()<\/code>\u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u8868\u793a\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u548c\u66ff\u6362\u540e\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world&quot;<\/p>\n<h2><strong>\u521b\u5efa\u6620\u5c04\u8868\uff0c\u5c06 &#39;h&#39; \u66ff\u6362\u4e3a &#39;H&#39;\uff0c&#39;w&#39; \u66ff\u6362\u4e3a &#39;W&#39;<\/strong><\/h2>\n<p>table = str.maketrans(&#39;hw&#39;, &#39;HW&#39;)<\/p>\n<p>new_text = text.translate(table)<\/p>\n<p>print(new_text)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6620\u5c04\u8868\uff0c\u5c06&#39;h&#39;\u66ff\u6362\u4e3a&#39;H&#39;\uff0c&#39;w&#39;\u66ff\u6362\u4e3a&#39;W&#39;\u3002\u7136\u540e\u4f7f\u7528translate()\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>re.sub()<\/code>\u65b9\u6cd5\u6765\u6267\u884c\u66ff\u6362\u64cd\u4f5c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">re.sub(pattern, repl, string, count=0, flags=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>pattern<\/code>\uff1a\u8981\u5339\u914d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/li>\n<li><code>repl<\/code>\uff1a\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>string<\/code>\uff1a\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>count<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u66ff\u6362\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a\u5168\u90e8\u66ff\u6362\u3002<\/li>\n<li><code>flags<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u63a7\u5236\u5339\u914d\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;The r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n in Spain falls mainly in the plain.&quot;<\/p>\n<h2><strong>\u5c06\u6240\u6709\u7684 &#39;a&#39; \u66ff\u6362\u4e3a &#39;o&#39;<\/strong><\/h2>\n<p>new_text = re.sub(r&#39;a&#39;, &#39;o&#39;, text)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The roin in Spoin folls moinly in the ploin.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709&#39;a&#39;\u66ff\u6362\u4e3a&#39;o&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u7684\u66ff\u6362\u65b9\u5f0f\uff0c\u4f8b\u5982\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u66ff\u6362\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u6761\u4ef6\u8fdb\u884c\u66ff\u6362\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u62fc\u63a5\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world&quot;<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>new_text = &#39;&#39;<\/p>\n<h2><strong>\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>for char in text:<\/p>\n<p>    # \u5982\u679c\u5b57\u7b26\u662f &#39;o&#39;\uff0c\u66ff\u6362\u4e3a &#39;O&#39;<\/p>\n<p>    if char == &#39;o&#39;:<\/p>\n<p>        new_text += &#39;O&#39;<\/p>\n<p>    else:<\/p>\n<p>        new_text += char<\/p>\n<p>print(new_text)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5c06\u6240\u6709\u7684&#39;o&#39;\u66ff\u6362\u4e3a&#39;O&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u4efb\u610f\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u5207\u7247\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u90e8\u5206\u5185\u5bb9<\/strong><\/h2>\n<p>new_text = text[:6] + &quot;Python&quot; + text[11:]<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>hello Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u90e8\u5206\u5185\u5bb9\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u7684\u524d6\u4e2a\u5b57\u7b26\u548c\u540e\u9762\u7684\u90e8\u5206\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u4e2d\u95f4\u63d2\u5165\u65b0\u7684\u5b57\u7b26\u4e32&quot;Python&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u66ff\u6362\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u6216<code>translate()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u4e2a replace() \u65b9\u6cd5\u4f9d\u6b21\u66ff\u6362<\/strong><\/h2>\n<p>new_text = text.replace(&quot;hello&quot;, &quot;hi&quot;).replace(&quot;world&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>hi Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u591a\u4e2a<code>replace()<\/code>\u65b9\u6cd5\u4f9d\u6b21\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<code>replace()<\/code>\u65b9\u6cd5\u3001<code>translate()<\/code>\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<code>replace()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u64cd\u4f5c\uff0c<code>translate()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u590d\u6742\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u5207\u7247\u6765\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5b57\u7b26\u4e32\u66ff\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u66ff\u6362\u7684\u65e7\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u662f\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u6307\u5b9a\u66ff\u6362\u7684\u6b21\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u4f1a\u66ff\u6362\u6240\u6709\u51fa\u73b0\u7684\u65e7\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, world! Hello, Python!&quot;\nnew_text = text.replace(&quot;Hello&quot;, &quot;Hi&quot;)\nprint(new_text)  # \u8f93\u51fa: Hi, world! Hi, Python!\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u9664\u4e86<code>str.replace()<\/code>\u65b9\u6cd5\uff0cPython\u8fd8\u63d0\u4f9b\u4e86<code>re<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u3002<code>re.sub()<\/code>\u51fd\u6570\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;The rain in Spain stays mainly in the plain.&quot;\nnew_text = re.sub(r&#39;in&#39;, &#39;on&#39;, text)\nprint(new_text)  # \u8f93\u51fa: The rain on Spain stays mainly on the plain.\n<\/code><\/pre>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u66ff\u6362\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>str.replace()<\/code>\u65b9\u6cd5\u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u66ff\u6362\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>re.sub()<\/code>\u548c<code>re.IGNORECASE<\/code>\u6807\u5fd7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;Hello, World! hello, world!&quot;\nnew_text = re.sub(r&#39;hello&#39;, &#39;Hi&#39;, text, flags=re.IGNORECASE)\nprint(new_text)  # \u8f93\u51fa: Hi, World! Hi, world!\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u5f62\u5f0f\u7684&quot;hello&quot;\u90fd\u88ab\u66ff\u6362\u4e3a&quot;Hi&quot;\uff0c\u65e0\u8bba\u5176\u5927\u5c0f\u5199\u5f62\u5f0f\u5982\u4f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecreplace()\u65b9\u6cd5\u3001translate()\u65b9\u6cd5\u548c\u6b63\u5219\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1112987,"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\/1112982"}],"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=1112982"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112982\/revisions"}],"predecessor-version":[{"id":1112990,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112982\/revisions\/1112990"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112987"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}