{"id":1031232,"date":"2024-12-31T11:23:48","date_gmt":"2024-12-31T03:23:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1031232.html"},"modified":"2024-12-31T11:23:50","modified_gmt":"2024-12-31T03:23:50","slug":"python-3-7%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1031232.html","title":{"rendered":"python 3.7\u4e2d\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0f3d5fa6-21c9-496a-8302-8f0db1613067.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python 3.7\u4e2d\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python 3.7\u4e2d\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u3001\u5b50\u5b57\u7b26\u4e32\u6216\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528\u66ff\u6362\u65b9\u6cd5<code>replace()<\/code>\u3001\u5207\u7247\u64cd\u4f5c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u7ffb\u8f6c\u548c\u62fc\u63a5\u7b49\u3002\u5e38\u7528\u7684\u662f<code>replace()<\/code>\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u66f4\u5f3a\u5927\u5730\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5220\u9664\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 replace() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>replace()<\/code> \u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u3002\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5220\u9664\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>modified_string = original_string.replace(&quot;World&quot;, &quot;&quot;)<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hello, !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7528\u7a7a\u5b57\u7b26\u4e32\u66ff\u6362\u4e86\u201cWorld\u201d\uff0c\u8fbe\u5230\u4e86\u5220\u9664\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662fPython\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u4e32\uff0c\u4ece\u800c\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>modified_string = original_string[:7] + original_string[12:]<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hello, !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u5220\u9664\u4e86\u5b57\u7b26\u4e32\u4e2d\u4ece\u7d22\u5f157\u523011\u7684\u90e8\u5206\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\uff08Regular Expressions\uff09\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5220\u9664\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_string = &quot;Hello, World!&quot;<\/p>\n<p>modified_string = re.sub(r&quot;World&quot;, &quot;&quot;, original_string)<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hello, !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>re.sub()<\/code>\u51fd\u6570\u6765\u5220\u9664\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>modified_string = original_string[0:5] + original_string[6:]<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hello World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u524d\u534a\u90e8\u5206\u548c\u540e\u534a\u90e8\u5206\uff0c\u6211\u4eec\u5220\u9664\u4e86\u7d22\u5f155\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 translate() \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>translate()<\/code> \u65b9\u6cd5\u7ed3\u5408<code>str.maketrans()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5220\u9664\u591a\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>remove_chars = &quot;oW&quot;<\/p>\n<p>translation_table = str.maketrans(&quot;&quot;, &quot;&quot;, remove_chars)<\/p>\n<p>modified_string = original_string.translate(translation_table)<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hell, rld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>str.maketrans()<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u7ffb\u8bd1\u8868\uff0c\u7136\u540e\u901a\u8fc7<code>translate()<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u201co\u201d\u548c\u201cW\u201d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u516d\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\u5e38\u5e38\u9700\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u3001<code>lstrip()<\/code>\u3001<code>rstrip()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;   Hello, World!   &quot;<\/p>\n<p>modified_string = original_string.strip()<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5220\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f7f\u7528<code>find()<\/code>\u65b9\u6cd5\u627e\u5230\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7136\u540e\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>substring = &quot;World&quot;<\/p>\n<p>start_index = original_string.find(substring)<\/p>\n<p>if start_index != -1:<\/p>\n<p>    end_index = start_index + len(substring)<\/p>\n<p>    modified_string = original_string[:start_index] + original_string[end_index:]<\/p>\n<p>    print(modified_string)  # \u8f93\u51fa: Hello, !<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7ffb\u8f6c\u548c\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u7279\u5b9a\u7684\u5220\u9664\u9700\u6c42\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u7ffb\u8f6c\u548c\u62fc\u63a5\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>substring = &quot;Hello&quot;<\/p>\n<p>if original_string.startswith(substring):<\/p>\n<p>    modified_string = original_string[len(substring):]<\/p>\n<p>    print(modified_string)  # \u8f93\u51fa: , World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u91cd\u590d\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5220\u9664\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_string = &quot;Hello, World!&quot;<\/p>\n<p>modified_string = &quot;&quot;.join(sorted(set(original_string), key=original_string.index))<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: Helo, Wrd!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_string = &quot;abc123def456ghi789&quot;<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>modified_string = re.sub(pattern, &quot;&quot;, original_string)<\/p>\n<p>print(modified_string)  # \u8f93\u51fa: abcdefghi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 3.7\u4e2d\uff0c\u5220\u9664\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5355\u5730\u66ff\u6362\u548c\u5220\u9664\u5b50\u5b57\u7b26\u4e32\uff0c\u5207\u7247\u64cd\u4f5c\u9002\u7528\u4e8e\u5220\u9664\u7279\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5219\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u9700\u6c42\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u5b57\u7b26\u4e32\u5220\u9664\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python 3.7\u4e2d\uff0c\u5220\u9664\u5b57\u7b26\u4e32\u662f\u5426\u610f\u5473\u7740\u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <code>replace()<\/code> \u65b9\u6cd5\u6765\u5220\u9664\u7279\u5b9a\u7684\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>my_string.replace(&quot;\u8981\u5220\u9664\u7684\u5b57\u7b26&quot;, &quot;&quot;)<\/code> \u53ef\u4ee5\u5c06\u76ee\u6807\u5b57\u7b26\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u80fd\u5426\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u6765\u5220\u9664\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff1f<\/strong><br \/>\u5207\u7247\u64cd\u4f5c\u662f\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u67d0\u4e2a\u5b50\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>new_string = original_string[:start_index] + original_string[end_index:]<\/code> \u53ef\u4ee5\u5220\u9664\u4ece <code>start_index<\/code> \u5230 <code>end_index<\/code> \u7684\u90e8\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u5728Python 3.7\u4e2d\u5220\u9664\u5b57\u7b26\u4e32\u7684\u6240\u6709\u7a7a\u683c\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\u6216\u8005 <code>join()<\/code> \u65b9\u6cd5\u7ed3\u5408 <code>split()<\/code> \u6765\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u683c\u3002\u4f7f\u7528 <code>my_string.replace(&quot; &quot;, &quot;&quot;)<\/code> \u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u6240\u6709\u7a7a\u683c\uff0c\u6216\u8005\u4f7f\u7528 <code>&#39;&#39;.join(my_string.split())<\/code> \u4e5f\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff0c\u540e\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u591a\u4e2a\u7a7a\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python 3.7\u4e2d\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u3001\u5b50\u5b57\u7b26\u4e32\u6216\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u66ff\u6362\u65b9\u6cd5replace()\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1031233,"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\/1031232"}],"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=1031232"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1031232\/revisions"}],"predecessor-version":[{"id":1031234,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1031232\/revisions\/1031234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1031233"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1031232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1031232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1031232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}