{"id":1087227,"date":"2025-01-08T13:33:06","date_gmt":"2025-01-08T05:33:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087227.html"},"modified":"2025-01-08T13:33:08","modified_gmt":"2025-01-08T05:33:08","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%ad%97%e7%ac%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087227.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200245\/1ca62483-a666-4009-b6e7-4a7dbc700d3c.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528translate()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7b49\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528replace()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>replace()\u65b9\u6cd5\u662fPython\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e2a\u5b50\u4e32\u3002\u901a\u8fc7\u5c06\u8981\u5220\u9664\u7684\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u5b57\u7b26\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>result = string.replace(&quot;o&quot;, &quot;&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell, Wrld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528replace()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u201co\u201d\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u201co\u201d\u5b57\u7b26\u7684\u76ee\u7684\u3002replace()\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\uff0c\u5219\u9700\u8981\u591a\u6b21\u8c03\u7528replace()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528translate()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>translate()\u65b9\u6cd5\u4e5f\u662fPython\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u7ed9\u5b9a\u7684\u5b57\u7b26\u6620\u5c04\u8868\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8fdb\u884c\u66ff\u6362\u6216\u5220\u9664\u3002\u4f7f\u7528translate()\u65b9\u6cd5\u5220\u9664\u5b57\u7b26\u65f6\uff0c\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u6620\u5c04\u8868\uff0c\u7136\u540e\u8c03\u7528translate()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>delete_chars = &quot;o,!&quot;<\/p>\n<p>translation_table = str.maketrans(&quot;&quot;, &quot;&quot;, delete_chars)<\/p>\n<p>result = string.translate(translation_table)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell Wrld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528str.maketrans()\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u7b26\u6620\u5c04\u8868\uff0c\u8be5\u6620\u5c04\u8868\u5c06\u5b57\u7b26\u201co\u201d\u3001\u201c,\u201d\u548c\u201c!\u201d\u6620\u5c04\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528translate()\u65b9\u6cd5\uff0c\u6839\u636e\u6620\u5c04\u8868\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u3002translate()\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u4e00\u6b21\u6027\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u63d0\u4f9b\u7684sub()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>pattern = &quot;[o,!]&quot;<\/p>\n<p>result = re.sub(pattern, &quot;&quot;, string)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell Wrld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u201c[o,!]\u201d\u5339\u914d\u5b57\u7b26\u201co\u201d\u3001\u201c,\u201d\u548c\u201c!\u201d\u3002\u7136\u540e\uff0c\u8c03\u7528re.sub()\u51fd\u6570\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u5b57\u7b26\u7684\u529f\u80fd\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u5f3a\uff0c\u53ef\u4ee5\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662fPython\u5b57\u7b26\u4e32\u5904\u7406\u7684\u57fa\u672c\u64cd\u4f5c\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\u3002\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>result = string[:4] + string[5:]<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u63d0\u53d6\u4e86\u5b57\u7b26\u4e32\u7684\u524d4\u4e2a\u5b57\u7b26\u548c\u7b2c5\u4e2a\u5b57\u7b26\u540e\u7684\u6240\u6709\u5b57\u7b26\uff0c\u5e76\u5c06\u5b83\u4eec\u62fc\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u7b2c5\u4e2a\u5b57\u7b26\u7684\u529f\u80fd\u3002\u5207\u7247\u64cd\u4f5c\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\uff0c\u5219\u6bd4\u8f83\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5269\u4e0b\u7684\u5b57\u7b26\u62fc\u63a5\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>delete_chars = &quot;o,!&quot;<\/p>\n<p>result = &#39;&#39;.join([char for char in string if char not in delete_chars])<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell Wrld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8be5\u5217\u8868\u5305\u542b\u4e86\u539f\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0d\u5728delete_chars\u4e2d\u7684\u5b57\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528join()\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u62fc\u63a5\u6210\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u6307\u5b9a\u5b57\u7b26\u7684\u529f\u80fd\u3002\u5217\u8868\u89e3\u6790\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5b57\u7b26\u8fc7\u6ee4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u5220\u9664\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5185\u7f6e\u65b9\u6cd5\u548c\u5de5\u5177\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5b57\u7b26\u5220\u9664\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_chars(string, chars_to_remove):<\/p>\n<p>    return &#39;&#39;.join(char for char in string if char not in chars_to_remove)<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>delete_chars = &quot;o,!&quot;<\/p>\n<p>result = remove_chars(string, delete_chars)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHell Wrld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aremove_chars()\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u548c\u8981\u5220\u9664\u7684\u5b57\u7b26\u96c6\u5408\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5217\u8868\u89e3\u6790\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u5305\u542b\u4e86\u539f\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u4e0d\u5728chars_to_remove\u96c6\u5408\u4e2d\u7684\u5b57\u7b26\u3002\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6307\u5b9a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u5b57\u7b26\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_char_at(string, index):<\/p>\n<p>    if index &lt; 0 or index &gt;= len(string):<\/p>\n<p>        return string<\/p>\n<p>    return string[:index] + string[index+1:]<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>index_to_remove = 7<\/p>\n<p>result = remove_char_at(string, index_to_remove)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello, Wrld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aremove_char_at()\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u5904\u7406\u7684\u5b57\u7b26\u4e32\u548c\u8981\u5220\u9664\u5b57\u7b26\u7684\u4f4d\u7f6e\u7d22\u5f15\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u524d\u90e8\u5206\u548c\u540e\u90e8\u5206\uff0c\u5e76\u5c06\u5b83\u4eec\u62fc\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u5b57\u7b26\u7684\u529f\u80fd\u3002\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\u7b49\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684strip()\u3001lstrip()\u548crstrip()\u65b9\u6cd5\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;  Hello, World!  \\n&quot;<\/p>\n<p>result = string.strip()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello, World!<\/p>\n<p>import re<\/p>\n<p>pattern = r&quot;\\s+&quot;<\/p>\n<p>result = re.sub(pattern, &quot;&quot;, string)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello,World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528strip()\u65b9\u6cd5\u5220\u9664\u4e86\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u201c\\s+\u201d\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u767d\u5b57\u7b26\uff0c\u5e76\u8c03\u7528re.sub()\u51fd\u6570\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u6240\u6709\u7a7a\u767d\u5b57\u7b26\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7<\/h3>\n<\/p>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u548c\u6587\u672c\u5206\u6790\u4e2d\uff0c\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u662f\u5e38\u89c1\u7684\u9884\u5904\u7406\u6b65\u9aa4\u4e4b\u4e00\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684translate()\u65b9\u6cd5\u6216\u8005\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>text = &quot;Hello, World! How&#39;s it going?&quot;<\/p>\n<p>translator = str.maketrans(&#39;&#39;, &#39;&#39;, string.punctuation)<\/p>\n<p>result = text.translate(translator)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello World Hows it going<\/p>\n<p>import re<\/p>\n<p>pattern = r&quot;[^\\w\\s]&quot;<\/p>\n<p>result = re.sub(pattern, &quot;&quot;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello World Hows it going<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528string.punctuation\u83b7\u53d6\u6240\u6709\u6807\u70b9\u7b26\u53f7\u5b57\u7b26\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u7b26\u6620\u5c04\u8868\uff0c\u5c06\u8fd9\u4e9b\u6807\u70b9\u7b26\u53f7\u6620\u5c04\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u6211\u4eec\u8c03\u7528translate()\u65b9\u6cd5\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6807\u70b9\u7b26\u53f7\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u201c[^\\w\\s]\u201d\u5339\u914d\u6240\u6709\u975e\u5355\u8bcd\u5b57\u7b26\u548c\u975e\u7a7a\u767d\u5b57\u7b26\uff0c\u5e76\u8c03\u7528re.sub()\u51fd\u6570\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5220\u9664\u6240\u6709\u6807\u70b9\u7b26\u53f7\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\uff0c\u53ea\u4fdd\u7559\u6bcf\u4e2a\u5b57\u7b26\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u548c\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_duplicate_chars(string):<\/p>\n<p>    seen = set()<\/p>\n<p>    return &#39;&#39;.join(seen.add(char) or char for char in string if char not in seen)<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>result = remove_duplicate_chars(string)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHelo, Wrld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aremove_duplicate_chars()\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u4e00\u4e2a\u96c6\u5408seen\u6765\u8ddf\u8e2a\u5df2\u7ecf\u9047\u5230\u7684\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u89e3\u6790\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6bcf\u4e2a\u5b57\u7b26\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u3002\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528translate()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u3001\u5220\u9664\u7a7a\u767d\u5b57\u7b26\u3001\u5220\u9664\u6807\u70b9\u7b26\u53f7\u548c\u5220\u9664\u91cd\u590d\u5b57\u7b26\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u7279\u5b9a\u5b57\u7b26\u3002\u53ea\u9700\u5c06\u8981\u5220\u9664\u7684\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>my_string.replace(&#39;a&#39;, &#39;&#39;)<\/code>\u5c06\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709&#39;a&#39;\u5b57\u7b26\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>join()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u5b9e\u73b0\u6b64\u76ee\u7684\uff0c\u6bd4\u5982<code>&#39;&#39;.join([c for c in my_string if c != &#39;a&#39;])<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u8981\u4fdd\u7559\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5b57\u7b26\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.translate()<\/code>\u65b9\u6cd5\u7ed3\u5408<code>str.maketrans()<\/code>\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7ffb\u8bd1\u8868\uff0c\u6307\u5b9a\u8981\u5220\u9664\u7684\u5b57\u7b26\uff0c\u7136\u540e\u4f7f\u7528<code>translate()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>my_string.translate(str.maketrans(&#39;&#39;, &#39;&#39;, &#39;abc&#39;))<\/code>\u5c06\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709&#39;a&#39;\u3001&#39;b&#39;\u548c&#39;c&#39;\u5b57\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u591a\u4e2a\u5b57\u7b26\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u521b\u5efa\u65b0\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u539f\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u539f\u5b57\u7b26\u4e32\u3002\u4efb\u4f55\u5220\u9664\u5b57\u7b26\u7684\u64cd\u4f5c\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u3002\u5982\u679c\u5e0c\u671b\u5c3d\u91cf\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>bytearray<\/code>\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u5904\u7406\u5b57\u7b26\uff0c\u4f46\u8fd9\u901a\u5e38\u4e0d\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002\u4f7f\u7528\u9ad8\u6548\u7684\u65b9\u6cd5\u5982<code>replace()<\/code>\u6216<code>translate()<\/code>\uff0c\u53ef\u4ee5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4fdd\u6301\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528translate( [&hellip;]","protected":false},"author":3,"featured_media":1087232,"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\/1087227"}],"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=1087227"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087227\/revisions"}],"predecessor-version":[{"id":1087235,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087227\/revisions\/1087235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087232"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}