{"id":973382,"date":"2024-12-27T05:56:33","date_gmt":"2024-12-26T21:56:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973382.html"},"modified":"2024-12-27T05:56:34","modified_gmt":"2024-12-26T21:56:34","slug":"python3%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973382.html","title":{"rendered":"python3\u5982\u4f55\u66ff\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195607\/1f3a45a8-4876-4607-9fdb-096981c35cdb.webp\" alt=\"python3\u5982\u4f55\u66ff\u6362\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\uff0c\u5b57\u7b26\u4e32\u66ff\u6362\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528str.replace()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0fre.sub()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u6a21\u677f\u66ff\u6362\u3002<\/strong>\u5176\u4e2d\uff0c<code>str.replace()<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001STR.REPLACE()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>str.replace()<\/code>\u662fPython\u4e2d\u6700\u7b80\u5355\u3001\u6700\u76f4\u63a5\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e00\u90e8\u5206\u66ff\u6362\u4e3a\u53e6\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u65b9\u6cd5\u6709\u4e24\u4e2a\u5fc5\u9700\u53c2\u6570\uff1a\u8981\u66ff\u6362\u7684\u65e7\u5b57\u7b26\u4e32\u548c\u66ff\u6362\u4e3a\u7684\u65b0\u5b57\u7b26\u4e32\uff0c\u8fd8\u6709\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\u662f\u66ff\u6362\u6b21\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p><code>str.replace(old, new)<\/code>\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684<code>old<\/code>\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a<code>new<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>new_text = text.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: Hello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684&quot;World&quot;\u88ab\u66ff\u6362\u6210\u4e86&quot;Python&quot;\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9650\u5b9a\u66ff\u6362\u6b21\u6570<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u7b2c\u4e09\u4e2a\u53c2\u6570<code>count<\/code>\uff0c\u4f60\u53ef\u4ee5\u9650\u5b9a\u66ff\u6362\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;banana banana banana&quot;<\/p>\n<p>new_text = text.replace(&quot;banana&quot;, &quot;apple&quot;, 2)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: apple apple banana<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u53ea\u6709\u524d\u4e24\u4e2a&quot;banana&quot;\u88ab\u66ff\u6362\u6210\u4e86&quot;apple&quot;\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001RE.SUB()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>re.sub()<\/code>\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u4f7f\u7528\u6a21\u5f0f\u5339\u914d\u7684\u65f6\u5019\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p><code>re.sub(pattern, repl, string)<\/code>\u7528\u4e8e\u5c06<code>string<\/code>\u4e2d\u6240\u6709\u5339\u914d<code>pattern<\/code>\u7684\u90e8\u5206\u66ff\u6362\u4e3a<code>repl<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\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&quot;<\/p>\n<p>new_text = re.sub(r&quot;ain&quot;, &quot;xxx&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: The rxxx in Spxxx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u5339\u914d\u6a21\u5f0f&quot;ain&quot;\u7684\u90e8\u5206\u90fd\u88ab\u66ff\u6362\u6210\u4e86&quot;xxx&quot;\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<\/ol>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;There are 2 apples and 5 bananas.&quot;<\/p>\n<p>new_text = re.sub(r&quot;\\d+&quot;, &quot;some&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: There are some apples and some bananas.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>\\d+<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u7b26\u4e32\u6a21\u677f\u66ff\u6362<\/p>\n<\/p>\n<p><p>Python\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u66ff\u6362\uff0c\u8fd9\u662f\u901a\u8fc7<code>string.Template<\/code>\u7c7b\u5b9e\u73b0\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u591a\u5904\u66ff\u6362\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u6a21\u677f\u8fdb\u884c\u66ff\u6362<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165<code>Template<\/code>\u7c7b\uff0c\u7136\u540e\u5b9a\u4e49\u4e00\u4e2a\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528<code>$<\/code>\u8fdb\u884c\u5360\u4f4d\uff1a<\/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: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>$name<\/code>\u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u4f7f\u7528<code>substitute()<\/code>\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u6279\u91cf\u66ff\u6362<\/li>\n<\/ol>\n<p><p><code>Template<\/code>\u7684<code>substitute()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u53c2\u6570\uff0c\u7528\u4e8e\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u5360\u4f4d\u7b26\uff1a<\/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: Hello, Alice! Welcome to Wonderland.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u6279\u91cf\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5728\u6587\u4ef6\u4e2d\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u66ff\u6362\u4e0d\u4ec5\u9650\u4e8e\u5185\u5b58\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u8fd8\u5e38\u5e38\u9700\u8981\u5bf9\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bfb\u53d6\u6587\u4ef6\u5e76\u66ff\u6362\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5148\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u6700\u540e\u5c06\u66ff\u6362\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>content = content.replace(&quot;old_string&quot;, &quot;new_string&quot;)<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u66ff\u6362\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>content = re.sub(r&quot;pattern&quot;, &quot;replacement&quot;, content)<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u66ff\u6362\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>new_fruits = [fruit.replace(&quot;a&quot;, &quot;o&quot;) for fruit in fruits]<\/p>\n<p>print(new_fruits)  # \u8f93\u51fa: [&#39;opple&#39;, &#39;bonono&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u7b80\u5355\u7684\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<\/ol>\n<p><p>\u540c\u6837\u5730\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408<code>re.sub()<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>new_fruits = [re.sub(r&quot;a&quot;, &quot;o&quot;, fruit) for fruit in fruits]<\/p>\n<p>print(new_fruits)  # \u8f93\u51fa: [&#39;opple&#39;, &#39;bonono&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u7684\u66ff\u6362\u89c4\u5219\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u66ff\u6362\u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u65f6\uff0c\u53ef\u4ee5\u904d\u5386\u5b57\u5178\u5e76\u5bf9\u6bcf\u4e2a\u503c\u8fdb\u884c\u66ff\u6362\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u904d\u5386\u5b57\u5178\u5e76\u66ff\u6362<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data = {&quot;name&quot;: &quot;Alice&quot;, &quot;place&quot;: &quot;Wonderland&quot;}<\/p>\n<p>new_data = {key: value.replace(&quot;a&quot;, &quot;o&quot;) for key, value in data.items()}<\/p>\n<p>print(new_data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Aloce&#39;, &#39;place&#39;: &#39;Wonderlond&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5bf9\u5b57\u5178\u4e2d\u7684\u6240\u6709\u503c\u8fdb\u884c\u7edf\u4e00\u66ff\u6362\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<\/ol>\n<p><p>\u540c\u6837\u5730\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>data = {&quot;name&quot;: &quot;Alice&quot;, &quot;place&quot;: &quot;Wonderland&quot;}<\/p>\n<p>new_data = {key: re.sub(r&quot;a&quot;, &quot;o&quot;, value) for key, value in data.items()}<\/p>\n<p>print(new_data)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Aloce&#39;, &#39;place&#39;: &#39;Wonderlond&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u66ff\u6362\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0cPython3\u4e3a\u5b57\u7b26\u4e32\u66ff\u6362\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u5404\u79cd\u66ff\u6362\u9700\u6c42\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u4e32\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u66ff\u6362\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u201capple\u201d\u66ff\u6362\u4e3a\u201corange\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">original_string = &quot;I like apple and apple pie.&quot;\nnew_string = original_string.replace(&quot;apple&quot;, &quot;orange&quot;)\nprint(new_string)  # \u8f93\u51fa: I like orange and orange pie.\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u66ff\u6362\u7684\u6b21\u6570\uff0c\u5982\u679c\u4e0d\u4f20\u5165\u8be5\u53c2\u6570\uff0c\u5219\u4f1a\u66ff\u6362\u6240\u6709\u5339\u914d\u9879\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u66ff\u6362\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u66ff\u6362\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>sub()<\/code>\u51fd\u6570\u3002\u6b64\u65b9\u6cd5\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5141\u8bb8\u60a8\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u66ff\u6362\u6240\u6709\u6570\u5b57\u4e3a\u201c#\u201d\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\noriginal_string = &quot;My phone number is 123-456-7890.&quot;\nnew_string = re.sub(r&#39;\\d+&#39;, &#39;#&#39;, original_string)\nprint(new_string)  # \u8f93\u51fa: My phone number is #-#-#.\n<\/code><\/pre>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u60a8\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u5b9a\u4e49\u9700\u8981\u66ff\u6362\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python3\u4e2d\u66ff\u6362\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5e76\u5e0c\u671b\u540c\u65f6\u66ff\u6362\u5176\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>replace()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u540c\u65f6\u66ff\u6362\u201capple\u201d\u548c\u201cbanana\u201d\uff1a  <\/p>\n<pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;apple pie&quot;, &quot;banana split&quot;]\nnew_fruits = [fruit.replace(&quot;apple&quot;, &quot;orange&quot;).replace(&quot;banana&quot;, &quot;grape&quot;) for fruit in fruits]\nprint(new_fruits)  # \u8f93\u51fa: [&#39;orange&#39;, &#39;grape&#39;, &#39;cherry&#39;, &#39;orange pie&#39;, &#39;grape split&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u591a\u4e2a\u66ff\u6362\uff0c\u4f7f\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\uff0c\u5b57\u7b26\u4e32\u66ff\u6362\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528str.replace()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0fre.s [&hellip;]","protected":false},"author":3,"featured_media":973389,"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\/973382"}],"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=973382"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973382\/revisions"}],"predecessor-version":[{"id":973392,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973382\/revisions\/973392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973389"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}