{"id":1141078,"date":"2025-01-08T22:27:49","date_gmt":"2025-01-08T14:27:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141078.html"},"modified":"2025-01-08T22:27:51","modified_gmt":"2025-01-08T14:27:51","slug":"python%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e9%83%a8%e5%88%86%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141078.html","title":{"rendered":"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103859\/a7c7e5d2-5b8e-4e6c-b48d-e14db02a0a36.webp\" alt=\"python\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>Python\u66ff\u6362\u5b57\u7b26\u4e32\u90e8\u5206\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528re\u6a21\u5757\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u3001\u5b57\u7b26\u4e32\u5207\u7247\u4e0e\u62fc\u63a5\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684<strong>replace()\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>Python \u7684 <code>replace()<\/code> \u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7b80\u5355\u6613\u7528\u3002\u5b83\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<code>str.replace(old, new[, maxreplace])<\/code>\uff0c\u5176\u4e2d <code>old<\/code> \u662f\u9700\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>new<\/code> \u662f\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>maxreplace<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u66ff\u6362\u6b21\u6570\u3002\u5982\u679c\u4e0d\u6307\u5b9a <code>maxreplace<\/code>\uff0c\u5219\u66ff\u6362\u6240\u6709\u51fa\u73b0\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>text = &quot;Hello World! Welcome to the World!&quot;<\/code>\uff0c\u6211\u4eec\u60f3\u5c06\u5176\u4e2d\u7684 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Universe&quot;\u3002\u53ef\u4ee5\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\u5b8c\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Welcome to the World!&quot;<\/p>\n<p>new_text = text.replace(&quot;World&quot;, &quot;Universe&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe! Welcome to the Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\uff0c\u5e76\u8be6\u7ec6\u89e3\u91ca\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 replace() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>replace() \u65b9\u6cd5<\/strong> \u662f\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u4e4b\u4e00\uff0c\u8bed\u6cd5\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u5b57\u7b26\u4e32\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>replace()<\/code> \u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.replace(old, new[, maxreplace])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>old<\/code> \u662f\u9700\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff1b<\/li>\n<li><code>new<\/code> \u662f\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32\uff1b<\/li>\n<li><code>maxreplace<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u66ff\u6362\u6b21\u6570\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Welcome to the World!&quot;<\/p>\n<p>new_text = text.replace(&quot;World&quot;, &quot;Universe&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe! Welcome to the Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6307\u5b9a\u66ff\u6362\u6b21\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>maxreplace<\/code> \u53c2\u6570\u6765\u9650\u5236\u66ff\u6362\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c\u53ea\u66ff\u6362\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684 &quot;World&quot;\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World! Welcome to the World!&quot;<\/p>\n<p>new_text = text.replace(&quot;World&quot;, &quot;Universe&quot;, 1)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe! Welcome to the World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 re \u6a21\u5757\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0cPython \u7684 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u51fd\u6570\u3002\u4f7f\u7528 <code>re.sub()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>re.sub()<\/code> \u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a\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> \u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff1b<\/li>\n<li><code>repl<\/code> \u662f\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\uff1b<\/li>\n<li><code>string<\/code> \u662f\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\uff1b<\/li>\n<li><code>count<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u66ff\u6362\u6b21\u6570\uff0c\u9ed8\u8ba4\u4e3a 0\uff0c\u8868\u793a\u66ff\u6362\u6240\u6709\u5339\u914d\u9879\uff1b<\/li>\n<li><code>flags<\/code> \u662f\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u63a7\u5236\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World! Welcome to the World!&quot;<\/p>\n<p>pattern = r&quot;World&quot;<\/p>\n<p>new_text = re.sub(pattern, &quot;Universe&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe! Welcome to the Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5339\u914d\u590d\u6742\u7684\u6a21\u5f0f\uff0c\u4f8b\u5982\u5339\u914d\u4ee5 &quot;W&quot; \u5f00\u5934\u7684\u5355\u8bcd\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a &quot;Universe&quot;\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello World! Welcome to the Wild West!&quot;<\/p>\n<p>pattern = r&quot;\\bW\\w+\\b&quot;<\/p>\n<p>new_text = re.sub(pattern, &quot;Universe&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe! Welcome to the Universe Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u7b26\u4e32\u5207\u7247\u4e0e\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u4e0e\u62fc\u63a5\u662f\u4e00\u79cd\u624b\u52a8\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u548c\u62fc\u63a5\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32 <code>text<\/code> \u4e2d\u7684 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Universe&quot;\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World!&quot;<\/p>\n<p>start = text.find(&quot;World&quot;)<\/p>\n<p>end = start + len(&quot;World&quot;)<\/p>\n<p>new_text = text[:start] + &quot;Universe&quot; + text[end:]<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u66ff\u6362\u6307\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u548c\u62fc\u63a5\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32 <code>text<\/code> \u4e2d\u4ece\u7d22\u5f15 6 \u5f00\u59cb\u7684 5 \u4e2a\u5b57\u7b26\u66ff\u6362\u4e3a &quot;Universe&quot;\uff1a<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World!&quot;<\/p>\n<p>start = 6<\/p>\n<p>length = 5<\/p>\n<p>new_text = text[:start] + &quot;Universe&quot; + text[start+length:]<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello Universe!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 translate() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>translate() \u65b9\u6cd5<\/strong> \u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u7684\u573a\u666f\u3002\u9700\u8981\u914d\u5408 <code>str.maketrans()<\/code> \u65b9\u6cd5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>str.maketrans()<\/code> \u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5b57\u7b26\u6620\u5c04\u8868\uff0c<code>translate()<\/code> \u65b9\u6cd5\u7528\u4e8e\u6839\u636e\u6620\u5c04\u8868\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World!&quot;<\/p>\n<p>translation_table = str.maketrans(&quot;HW&quot;, &quot;hw&quot;)<\/p>\n<p>new_text = text.translate(translation_table)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;hello world!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u66ff\u6362\u591a\u4e2a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>str.maketrans()<\/code> \u65b9\u6cd5\u521b\u5efa\u590d\u6742\u7684\u5b57\u7b26\u6620\u5c04\u8868\uff0c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World!&quot;<\/p>\n<p>translation_table = str.maketrans(&quot;HW&quot;, &quot;hw&quot;, &quot;!&quot;)<\/p>\n<p>new_text = text.translate(translation_table)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;hello world&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u5b57\u7b26\u66ff\u6362\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u9ad8\u7ea7\u529f\u80fd\u8fdb\u884c\u590d\u6742\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u4f8b\u5982\u4f7f\u7528\u6355\u83b7\u7ec4\u548c\u66ff\u6362\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6355\u83b7\u7ec4\u8fdb\u884c\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6355\u83b7\u7ec4\uff08capture groups\uff09\u63d0\u53d6\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u5f15\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;The quick brown fox jumps over the lazy dog.&quot;<\/p>\n<p>pattern = r&quot;(\\b\\w+\\b)&quot;<\/p>\n<p>new_text = re.sub(pattern, r&quot;(\\1)&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;(The) (quick) (brown) (fox) (jumps) (over) (the) (lazy) (dog).&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u66ff\u6362\u51fd\u6570\u8fdb\u884c\u52a8\u6001\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u66ff\u6362\u51fd\u6570\uff08replacement function\uff09\u6839\u636e\u5339\u914d\u5185\u5bb9\u52a8\u6001\u751f\u6210\u66ff\u6362\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def replace(match):<\/p>\n<p>    word = match.group(0)<\/p>\n<p>    return word[::-1]<\/p>\n<p>text = &quot;The quick brown fox jumps over the lazy dog.&quot;<\/p>\n<p>pattern = r&quot;\\b\\w+\\b&quot;<\/p>\n<p>new_text = re.sub(pattern, replace, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;ehT kciuq nworb xof spmuj revo eht yzal god.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6a21\u677f\uff08string template\uff09\u662f\u4e00\u79cd\u66f4\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u52a8\u6001\u5185\u5bb9\u7684\u573a\u666f\u3002Python \u63d0\u4f9b\u4e86 <code>string.Template<\/code> \u7c7b\u6765\u5904\u7406\u5b57\u7b26\u4e32\u6a21\u677f\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>string.Template<\/code> \u7c7b\u5b9a\u4e49\u5b57\u7b26\u4e32\u6a21\u677f\uff0c\u5e76\u4f7f\u7528 <code>substitute()<\/code> \u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Hello, $name!&quot;)<\/p>\n<p>new_text = template.substitute(name=&quot;Alice&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello, Alice!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u6279\u91cf\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63d0\u4f9b\u591a\u4e2a\u66ff\u6362\u9879\uff0c\u8fdb\u884c\u6279\u91cf\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Hello, $name! Welcome to $place.&quot;)<\/p>\n<p>data = {&quot;name&quot;: &quot;Alice&quot;, &quot;place&quot;: &quot;Wonderland&quot;}<\/p>\n<p>new_text = template.substitute(data)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello, Alice! Welcome to Wonderland.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u65f6\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\u4f18\u5316\u548c\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u9ad8\u6548\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>replace()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u66ff\u6362\uff0c<code>re.sub()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u590d\u6742\u66ff\u6362\u3002<\/li>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u66ff\u6362<\/strong>\uff1a\u5728\u66ff\u6362\u524d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u9700\u8981\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u66ff\u6362\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World!&quot;<\/p>\n<p>if &quot;World&quot; in text:<\/p>\n<p>    new_text = text.replace(&quot;World&quot;, &quot;Universe&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5904\u7406\u8fb9\u754c\u60c5\u51b5<\/strong>\uff1a\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u4f8b\u5982\u7a7a\u5b57\u7b26\u4e32\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u3002<\/li>\n<li><strong>\u907f\u514d\u65e0\u9650\u5faa\u73af<\/strong>\uff1a\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u66ff\u6362\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;a&quot;*1000<\/p>\n<p>pattern = r&quot;(a*)&quot;<\/p>\n<p>new_text = re.sub(pattern, r&quot;\\1b&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;abbbbbbbbbbbbbbbbbbbbbbbbb...&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u8bb8\u591a\u5e38\u89c1\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6587\u672c\u6e05\u7406\u3001\u6570\u636e\u683c\u5f0f\u5316\u3001\u6a21\u677f\u751f\u6210\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6587\u672c\u6e05\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u6e05\u7406\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u53bb\u9664\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u6216\u683c\u5f0f\u5316\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, World! Welcome to 2023.&quot;<\/p>\n<p>pattern = r&quot;[^\\w\\s]&quot;<\/p>\n<p>new_text = re.sub(pattern, &quot;&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hello World Welcome to 2023&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u683c\u5f0f\u5316\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = &quot;2023-10-01&quot;<\/p>\n<p>formatted_data = data.replace(&quot;-&quot;, &quot;\/&quot;)<\/p>\n<p>print(formatted_data)  # \u8f93\u51fa: &quot;2023\/10\/01&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6a21\u677f\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u677f\u751f\u6210\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u6a21\u677f\u65b9\u6cd5\u52a8\u6001\u751f\u6210\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from string import Template<\/p>\n<p>template = Template(&quot;Dear $name,\\n\\nWelcome to $company.&quot;)<\/p>\n<p>data = {&quot;name&quot;: &quot;Alice&quot;, &quot;company&quot;: &quot;TechCorp&quot;}<\/p>\n<p>new_text = template.substitute(data)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u5185\u5bb9\u662fPython\u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec <code>replace()<\/code> \u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u5207\u7247\u4e0e\u62fc\u63a5\u3001<code>translate()<\/code> \u65b9\u6cd5\u3001\u5b57\u7b26\u4e32\u6a21\u677f\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u66ff\u6362\u9700\u6c42\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>replace()<\/code> \u65b9\u6cd5\u6765\u66ff\u6362\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 <code>text = &quot;Hello World&quot;<\/code>\uff0c\u5e76\u60f3\u5c06 &quot;World&quot; \u66ff\u6362\u4e3a &quot;Python&quot;\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>text.replace(&quot;World&quot;, &quot;Python&quot;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de <code>&quot;Hello Python&quot;<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u5185\u5bb9\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u66ff\u6362\uff0c\u6bd4\u5982\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>re<\/code> \u6a21\u5757\u3002\u901a\u8fc7 <code>re.sub()<\/code> \u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5185\u5bb9\u8fdb\u884c\u66ff\u6362\u3002\u4f8b\u5982\uff0c<code>re.sub(r&#39;\\d+&#39;, &#39;number&#39;, &#39;There are 2 apples and 3 oranges&#39;)<\/code> \u5c06\u4f1a\u628a\u6240\u6709\u6570\u5b57\u66ff\u6362\u4e3a &quot;number&quot;\uff0c\u7ed3\u679c\u4e3a &quot;There are number apples and number oranges&quot;\u3002<\/p>\n<p><strong>\u5982\u4f55\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u4e0d\u540c\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\u66ff\u6362\u591a\u4e2a\u90e8\u5206\u53ef\u4ee5\u901a\u8fc7\u591a\u6b21\u8c03\u7528 <code>replace()<\/code> \u65b9\u6cd5\uff0c\u6216\u8005\u4f7f\u7528 <code>re.sub()<\/code> \u7ed3\u5408\u4e00\u4e2a\u5b57\u5178\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>replace()<\/code> \u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>text.replace(&quot;apple&quot;, &quot;orange&quot;).replace(&quot;banana&quot;, &quot;kiwi&quot;)<\/code>\u3002\u800c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u5b57\u5178\u4e2d\u7684\u66ff\u6362\u9879\uff0c\u4f9d\u6b21\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u66ff\u6362\u5b57\u7b26\u4e32\u90e8\u5206\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528re\u6a21\u5757\u8fdb\u884c\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u3001\u5b57\u7b26\u4e32\u5207 [&hellip;]","protected":false},"author":3,"featured_media":1141088,"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\/1141078"}],"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=1141078"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141078\/revisions"}],"predecessor-version":[{"id":1141092,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141078\/revisions\/1141092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141088"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}