{"id":1120149,"date":"2025-01-08T18:53:14","date_gmt":"2025-01-08T10:53:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120149.html"},"modified":"2025-01-08T18:53:16","modified_gmt":"2025-01-08T10:53:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120149.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082926\/fc442871-2104-4f66-baa3-da9e7d7a5a8b.webp\" alt=\"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u5143\u7d20\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709<strong>\u7d22\u5f15\u3001\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u7b49\u3002\u7d22\u5f15\u548c\u5207\u7247\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7d22\u5f15<\/h2>\n<\/p>\n<p><p>\u7d22\u5f15\u662f\u4e00\u79cd\u76f4\u63a5\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u65b9\u6cd5\u3002Python\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>1. \u6b63\u5411\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u6b63\u5411\u7d22\u5f15\u4ece0\u5f00\u59cb\u8ba1\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string[0])  # \u8f93\u51fa &#39;H&#39;<\/p>\n<p>print(string[7])  # \u8f93\u51fa &#39;W&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u53cd\u5411\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u53cd\u5411\u7d22\u5f15\u4ece-1\u5f00\u59cb\u8ba1\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string[-1])  # \u8f93\u51fa &#39;!&#39;<\/p>\n<p>print(string[-5])  # \u8f93\u51fa &#39;o&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5207\u7247<\/h2>\n<\/p>\n<p><p>\u5207\u7247\u7528\u4e8e\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u5207\u7247\u8bed\u6cd5\u4e3a<code>string[start:end:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\u8be5\u7d22\u5f15\u5904\u7684\u5b57\u7b26\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u672c\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u5207\u7247\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string[0:5])  # \u8f93\u51fa &#39;Hello&#39;<\/p>\n<p>print(string[7:12])  # \u8f93\u51fa &#39;World&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6b65\u957f\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u6b65\u957f\u7528\u4e8e\u6307\u5b9a\u63d0\u53d6\u5b57\u7b26\u7684\u95f4\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string[0:12:2])  # \u8f93\u51fa &#39;Hlo ol&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7701\u7565\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7701\u7565<code>start<\/code>\u3001<code>end<\/code>\u6216<code>step<\/code>\u53c2\u6570\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string[:5])   # \u8f93\u51fa &#39;Hello&#39;<\/p>\n<p>print(string[7:])   # \u8f93\u51fa &#39;World!&#39;<\/p>\n<p>print(string[::2])  # \u8f93\u51fa &#39;Hlo ol!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u3001\u63d0\u53d6\u548c\u66ff\u6362\u6a21\u5f0f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h3>1. \u5339\u914d\u5355\u4e2a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5355\u4e2a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>pattern = r&#39;[A-Z]&#39;  # \u5339\u914d\u5927\u5199\u5b57\u6bcd<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(matches)  # \u8f93\u51fa [&#39;H&#39;, &#39;W&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u63d0\u53d6\u5b50\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l: example@example.com&quot;<\/p>\n<p>pattern = r&#39;\\b\\w+@\\w+\\.\\w+\\b&#39;  # \u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/p>\n<p>matches = re.findall(pattern, string)<\/p>\n<p>print(matches)  # \u8f93\u51fa [&#39;example@example.com&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u3001\u66ff\u6362\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>1. <code>find<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>find<\/code>\u65b9\u6cd5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u4f4d\u7f6e\u3002\u5982\u679c\u672a\u627e\u5230\u5b50\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de-1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>index = string.find(&quot;World&quot;)<\/p>\n<p>print(index)  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. <code>split<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>split<\/code>\u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>fruits = string.split(&#39;,&#39;)<\/p>\n<p>print(fruits)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. <code>join<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>join<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>string = &#39;,&#39;.join(fruits)<\/p>\n<p>print(string)  # \u8f93\u51fa &#39;apple,banana,cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. <code>replace<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>replace<\/code>\u65b9\u6cd5\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>new_string = string.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;Hello, Python!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5. <code>strip<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>strip<\/code>\u65b9\u6cd5\u7528\u4e8e\u79fb\u9664\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u6307\u5b9a\u5b57\u7b26\uff08\u9ed8\u8ba4\u4e3a\u7a7a\u683c\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;   Hello, World!   &quot;<\/p>\n<p>new_string = string.strip()<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6. <code>startswith<\/code>\u548c<code>endswith<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u6307\u5b9a\u7684\u524d\u7f00\u6216\u540e\u7f00\u5f00\u5934\u6216\u7ed3\u5c3e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>print(string.startswith(&quot;Hello&quot;))  # \u8f93\u51fa True<\/p>\n<p>print(string.endswith(&quot;World!&quot;))   # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7. <code>count<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>count<\/code>\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World! Hello, Python!&quot;<\/p>\n<p>count = string.count(&quot;Hello&quot;)<\/p>\n<p>print(count)  # \u8f93\u51fa 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8. <code>format<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/p>\n<p>string = &quot;My name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(string)  # \u8f93\u51fa &#39;My name is John and I am 30 years old.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5b57\u7b26\u4e32\u62fc\u63a5<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u591a\u4e2a\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5143\u7d20\u5e76\u5c06\u5b83\u4eec\u62fc\u63a5\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>+<\/code>\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>+<\/code>\u64cd\u4f5c\u7b26\u62fc\u63a5\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string1 = &quot;Hello&quot;<\/p>\n<p>string2 = &quot;World&quot;<\/p>\n<p>new_string = string1 + &quot;, &quot; + string2 + &quot;!&quot;<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>join<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>join<\/code>\u65b9\u6cd5\u5728\u62fc\u63a5\u591a\u4e2a\u5b57\u7b26\u4e32\u65f6\u66f4\u52a0\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u62fc\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">strings = [&quot;Hello&quot;, &quot;World&quot;, &quot;!&quot;]<\/p>\n<p>new_string = &#39; &#39;.join(strings)<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;Hello World !&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5b57\u7b26\u4e32\u8fed\u4ee3<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u6765\u9010\u4e2a\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>for<\/code>\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u8fed\u4ee3\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello&quot;<\/p>\n<p>for char in string:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u5728\u8fed\u4ee3\u5b57\u7b26\u4e32\u65f6\u63d0\u4f9b\u7d22\u5f15\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello&quot;<\/p>\n<p>for index, char in enumerate(string):<\/p>\n<p>    print(f&quot;Index: {index}, Character: {char}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u5b57\u7b26\u4e32\u5206\u5272\u4e0e\u7ec4\u5408<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u91cd\u65b0\u7ec4\u5408\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>1. \u5206\u5272\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p><code>split<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>parts = string.split(&#39;,&#39;)<\/p>\n<p>print(parts)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7ec4\u5408\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u5c06\u5206\u5272\u7684\u90e8\u5206\u91cd\u65b0\u7ec4\u5408\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">parts = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>new_string = &#39;,&#39;.join(parts)<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;apple,banana,cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u66ff\u6362\u548c\u5206\u5272\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u5148\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\uff0c\u7136\u540e\u518d\u5206\u5272\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple|banana|cherry&quot;<\/p>\n<p>new_string = string.replace(&#39;|&#39;, &#39;,&#39;)<\/p>\n<p>parts = new_string.split(&#39;,&#39;)<\/p>\n<p>print(parts)  # \u8f93\u51fa [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u6839\u636e\u7279\u5b9a\u683c\u5f0f\u751f\u6210\u65b0\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/p>\n<p>string = &quot;My name is %s and I am %d years old.&quot; % (name, age)<\/p>\n<p>print(string)  # \u8f93\u51fa &#39;My name is John and I am 30 years old.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>format<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u683c\u5f0f\u5316\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/p>\n<p>string = &quot;My name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(string)  # \u8f93\u51fa &#39;My name is John and I am 30 years old.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528f-string\uff08Python 3.6+\uff09<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u7b80\u6d01\u8bed\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;John&quot;<\/p>\n<p>age = 30<\/p>\n<p>string = f&quot;My name is {name} and I am {age} years old.&quot;<\/p>\n<p>print(string)  # \u8f93\u51fa &#39;My name is John and I am 30 years old.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u5b57\u7b26\u4e32\u67e5\u627e\u4e0e\u5339\u914d<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u67e5\u627e\u548c\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u662f\u5e38\u89c1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>find<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>find<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u7d22\u5f15\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>index = string.find(&quot;World&quot;)<\/p>\n<p>print(index)  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528<code>re.search<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>re.search<\/code>\u51fd\u6570\u7528\u4e8e\u67e5\u627e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>pattern = r&#39;World&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(match.start())  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u64cd\u4f5c\u7b26\u7528\u4e8e\u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>result = &quot;World&quot; in string<\/p>\n<p>print(result)  # \u8f93\u51fa True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u5b57\u7b26\u4e32\u66ff\u6362<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u662f\u4fee\u6539\u5b57\u7b26\u4e32\u5185\u5bb9\u7684\u5e38\u89c1\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>new_string = string.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;Hello, Python!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u66ff\u6362\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;The rain in Spain&quot;<\/p>\n<p>pattern = r&#39;\\b\\w+ain\\b&#39;<\/p>\n<p>new_string = re.sub(pattern, &quot;rain&quot;, string)<\/p>\n<p>print(new_string)  # \u8f93\u51fa &#39;The rain in rain&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u63d0\u53d6\u5b57\u7b26\u4e32\u5143\u7d20\u662fPython\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86<strong>\u7d22\u5f15\u3001\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u7b49\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u638c\u63e1Python\u5b57\u7b26\u4e32\u64cd\u4f5c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\uff08slicing\uff09\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>text = &quot;Hello, World!&quot;<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>text[0:5]<\/code>\u63d0\u53d6\u51fa\u201cHello\u201d\u3002\u5207\u7247\u7684\u8bed\u6cd5\u662f<code>string[start:end]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u5f00\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>find()<\/code>\u6216<code>index()<\/code>\u65b9\u6cd5\u3002<code>find()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\uff1b\u800c<code>index()<\/code>\u65b9\u6cd5\u5728\u672a\u627e\u5230\u65f6\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<code>text.find(&quot;World&quot;)<\/code>\u5c06\u8fd4\u56de7\uff0c\u56e0\u4e3a\u201cWorld\u201d\u4ece\u7d22\u5f157\u5f00\u59cb\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6216\u7279\u5b9a\u6a21\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6216\u7279\u5b9a\u6a21\u5f0f\u3002\u901a\u8fc7<code>re.findall()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5b50\u4e32\u3002\u4f8b\u5982\uff0c<code>import re; re.findall(r&#39;\\d+&#39;, &quot;There are 2 cats and 3 dogs&quot;)<\/code>\u5c06\u8fd4\u56de<code>[&#39;2&#39;, &#39;3&#39;]<\/code>\uff0c\u63d0\u53d6\u51fa\u6240\u6709\u6570\u5b57\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5339\u914d\u80fd\u529b\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u63d0\u53d6\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u7d22\u5f15\u3001\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u7b49\u3002\u7d22\u5f15\u548c\u5207\u7247\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u6b63\u5219 [&hellip;]","protected":false},"author":3,"featured_media":1120157,"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\/1120149"}],"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=1120149"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120149\/revisions"}],"predecessor-version":[{"id":1120158,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120149\/revisions\/1120158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120157"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}