{"id":1141292,"date":"2025-01-08T22:29:34","date_gmt":"2025-01-08T14:29:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141292.html"},"modified":"2025-01-08T22:29:36","modified_gmt":"2025-01-08T14:29:36","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e6%88%902%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141292.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u62102\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104027\/559df61a-b506-45cf-a234-e1364dc0cb86.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u62102\u884c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u884c\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u6587\u672c\u5904\u7406\u5e93\u7b49\u3002\u4ee5\u4e0b\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5split()\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528split()\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u90e8\u5206\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a1. \u786e\u5b9a\u5206\u9694\u7b26\uff0c2. \u4f7f\u7528split()\u51fd\u6570\u5206\u5272\u5b57\u7b26\u4e32\uff0c3. \u5c06\u7ed3\u679c\u5b58\u5165\u53d8\u91cf\u4e2d\u3002<\/strong> \u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>split_string = string.split(&quot;, &quot;)<\/p>\n<p>print(split_string[0])<\/p>\n<p>print(split_string[1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u9017\u53f7\u548c\u7a7a\u683c <code>&quot;, &quot;<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5206\u522b\u662f &quot;Hello&quot; \u548c &quot;World!&quot;\uff0c\u5e76\u88ab\u6253\u5370\u5728\u4e24\u884c\u4e0a\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528split()\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>split() \u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u662f\u4efb\u4f55\u7a7a\u767d\u5b57\u7b26\uff08\u5305\u62ec\u7a7a\u683c\u3001\u6362\u884c\u7b26\u3001\u5236\u8868\u7b26\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>split_string = string.split(&quot;, &quot;)<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab <code>&quot;, &quot;<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u7684\u4e24\u4e2a\u5143\u7d20\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>split() \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u4efb\u610f\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7834\u6298\u53f7 <code>&quot;-&quot;<\/code>\u3001\u70b9 <code>&quot;.&quot;<\/code> \u6216\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello-World!&quot;<\/p>\n<p>split_string = string.split(&quot;-&quot;)<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u7834\u6298\u53f7 <code>&quot;-&quot;<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u7684\u4e24\u4e2a\u5143\u7d20\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>Python\u7684 <code>re<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5206\u5272\u64cd\u4f5c\u3002<code>re.split()<\/code> \u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u9694\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>split_string = re.split(r&#39;,\\s*&#39;, string)<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>r&#39;,\\s*&#39;<\/code> \u5339\u914d\u9017\u53f7\u548c\u4efb\u610f\u6570\u91cf\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u5c06\u5b57\u7b26\u4e32 <code>string<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u590d\u6742\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u66f4\u590d\u6742\u7684\u5206\u9694\u7b26\uff0c\u4f8b\u5982\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u7279\u5b9a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;Hello---World!&quot;<\/p>\n<p>split_string = re.split(r&#39;-+&#39;, string)<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>r&#39;-+&#39;<\/code> \u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u7834\u6298\u53f7\uff0c\u5c06\u5b57\u7b26\u4e32 <code>string<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528textwrap\u6a21\u5757\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>Python\u7684 <code>textwrap<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u7528\u4e8e\u683c\u5f0f\u5316\u6587\u672c\u6bb5\u843d\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002<code>textwrap.wrap()<\/code> \u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5bbd\u5ea6\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>wrapped_string = textwrap.wrap(string, width=7)<\/p>\n<p>print(wrapped_string[0])  # \u8f93\u51fa: Hello,<\/p>\n<p>print(wrapped_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u5206\u5272\u6210\u6bcf\u884c\u6700\u591a\u5305\u542b7\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h2>2. \u8bbe\u7f6e\u4e0d\u540c\u7684\u5bbd\u5ea6<\/h2>\n<\/p>\n<p><p><code>textwrap.wrap()<\/code> \u51fd\u6570\u5141\u8bb8\u6211\u4eec\u8bbe\u7f6e\u4e0d\u540c\u7684\u5bbd\u5ea6\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>wrapped_string = textwrap.wrap(string, width=5)<\/p>\n<p>print(wrapped_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(wrapped_string[1])  # \u8f93\u51fa: , Wor<\/p>\n<p>print(wrapped_string[2])  # \u8f93\u51fa: ld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u5206\u5272\u6210\u6bcf\u884c\u6700\u591a\u5305\u542b5\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684Python\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c <code>split()<\/code> \u65b9\u6cd5\u7ed3\u5408\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>split_string = [line for line in string.split(&quot;, &quot;)]<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab <code>&quot;, &quot;<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5e76\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u5e76\u7ed3\u5408\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello-World!&quot;<\/p>\n<p>split_string = [line for line in string.split(&quot;-&quot;)]<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u7834\u6298\u53f7 <code>&quot;-&quot;<\/code> \u5206\u9694\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5e76\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u91cf\u7684\u5b57\u7b26\u4e32\u5206\u5272\u3002\u751f\u6210\u5668\u53ef\u4ee5\u9010\u6b65\u751f\u6210\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u5b57\u7b26\u4e32\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def split_string(string, delimiter):<\/p>\n<p>    part = &quot;&quot;<\/p>\n<p>    for char in string:<\/p>\n<p>        if char != delimiter:<\/p>\n<p>            part += char<\/p>\n<p>        else:<\/p>\n<p>            yield part<\/p>\n<p>            part = &quot;&quot;<\/p>\n<p>    yield part<\/p>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<p>for part in split_string(string, &quot;, &quot;):<\/p>\n<p>    print(part)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668 <code>split_string<\/code> \u6839\u636e\u5206\u9694\u7b26 <code>&quot;, &quot;<\/code> \u9010\u6b65\u751f\u6210\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u751f\u6210\u5668\u7684\u5206\u9694\u7b26\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def split_string(string, delimiter):<\/p>\n<p>    part = &quot;&quot;<\/p>\n<p>    for char in string:<\/p>\n<p>        if char != delimiter:<\/p>\n<p>            part += char<\/p>\n<p>        else:<\/p>\n<p>            yield part<\/p>\n<p>            part = &quot;&quot;<\/p>\n<p>    yield part<\/p>\n<p>string = &quot;Hello-World!&quot;<\/p>\n<p>for part in split_string(string, &quot;-&quot;):<\/p>\n<p>    print(part)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668 <code>split_string<\/code> \u6839\u636e\u5206\u9694\u7b26 <code>&quot;-&quot;<\/code> \u9010\u6b65\u751f\u6210\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684partition()\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>partition()<\/code> \u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e09\u4e2a\u90e8\u5206\uff1a\u5206\u9694\u7b26\u524d\u7684\u90e8\u5206\u3001\u5206\u9694\u7b26\u672c\u8eab\u3001\u5206\u9694\u7b26\u540e\u7684\u90e8\u5206\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u53ea\u9700\u8981\u5206\u5272\u4e00\u6b21\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>before, delimiter, after = string.partition(&quot;, &quot;)<\/p>\n<p>print(before)      # \u8f93\u51fa: Hello<\/p>\n<p>print(delimiter)   # \u8f93\u51fa: , <\/p>\n<p>print(after)       # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab <code>&quot;, &quot;<\/code> \u5206\u9694\u6210\u4e09\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u53d8\u91cf <code>before<\/code>\u3001<code>delimiter<\/code> \u548c <code>after<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u5e76\u7ed3\u5408 <code>partition()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello-World!&quot;<\/p>\n<p>before, delimiter, after = string.partition(&quot;-&quot;)<\/p>\n<p>print(before)      # \u8f93\u51fa: Hello<\/p>\n<p>print(delimiter)   # \u8f93\u51fa: -<\/p>\n<p>print(after)       # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u7834\u6298\u53f7 <code>&quot;-&quot;<\/code> \u5206\u9694\u6210\u4e09\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u53d8\u91cf <code>before<\/code>\u3001<code>delimiter<\/code> \u548c <code>after<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684rpartition()\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>rpartition()<\/code> \u65b9\u6cd5\u7c7b\u4f3c\u4e8e <code>partition()<\/code> \u65b9\u6cd5\uff0c\u4f46\u5b83\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u641c\u7d22\u5206\u9694\u7b26\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ece\u53f3\u4fa7\u8fdb\u884c\u5206\u5272\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello, World!&quot;<\/p>\n<p>before, delimiter, after = string.rpartition(&quot;, &quot;)<\/p>\n<p>print(before)      # \u8f93\u51fa: Hello<\/p>\n<p>print(delimiter)   # \u8f93\u51fa: , <\/p>\n<p>print(after)       # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab <code>&quot;, &quot;<\/code> \u4ece\u53f3\u4fa7\u5206\u9694\u6210\u4e09\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u53d8\u91cf <code>before<\/code>\u3001<code>delimiter<\/code> \u548c <code>after<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u5e76\u7ed3\u5408 <code>rpartition()<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u4ece\u53f3\u4fa7\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello-World!&quot;<\/p>\n<p>before, delimiter, after = string.rpartition(&quot;-&quot;)<\/p>\n<p>print(before)      # \u8f93\u51fa: Hello<\/p>\n<p>print(delimiter)   # \u8f93\u51fa: -<\/p>\n<p>print(after)       # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u88ab\u7834\u6298\u53f7 <code>&quot;-&quot;<\/code> \u4ece\u53f3\u4fa7\u5206\u9694\u6210\u4e09\u4e2a\u90e8\u5206\uff0c\u5e76\u5206\u522b\u5b58\u50a8\u5728\u53d8\u91cf <code>before<\/code>\u3001<code>delimiter<\/code> \u548c <code>after<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684splitlines()\u65b9\u6cd5\u8fdb\u884c\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>splitlines()<\/code> \u65b9\u6cd5\u6839\u636e\u6362\u884c\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u591a\u884c\u6587\u672c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello\\nWorld!&quot;<\/p>\n<p>split_string = string.splitlines()<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u6839\u636e\u6362\u884c\u7b26 <code>&quot;\\n&quot;<\/code> \u5206\u9694\u6210\u591a\u884c\uff0c\u5e76\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u4fdd\u7559\u6362\u884c\u7b26<\/h2>\n<\/p>\n<p><p><code>splitlines()<\/code> \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u9009\u62e9\u662f\u5426\u4fdd\u7559\u6362\u884c\u7b26\u3002\u901a\u8fc7\u4f20\u9012\u53c2\u6570 <code>keepends=True<\/code>\uff0c\u53ef\u4ee5\u4fdd\u7559\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;Hello\\nWorld!&quot;<\/p>\n<p>split_string = string.splitlines(True)<\/p>\n<p>print(split_string[0])  # \u8f93\u51fa: Hello\\n<\/p>\n<p>print(split_string[1])  # \u8f93\u51fa: World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>string<\/code> \u6839\u636e\u6362\u884c\u7b26 <code>&quot;\\n&quot;<\/code> \u5206\u9694\u6210\u591a\u884c\uff0c\u5e76\u5b58\u50a8\u5728\u5217\u8868 <code>split_string<\/code> \u4e2d\uff0c\u540c\u65f6\u4fdd\u7559\u4e86\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u516b\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u884c\u6216\u591a\u884c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b9e\u9645\u9700\u6c42\u3002\u4f8b\u5982\uff0c<code>split()<\/code> \u65b9\u6cd5\u9002\u5408\u7b80\u5355\u7684\u5206\u9694\u64cd\u4f5c\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u5408\u590d\u6742\u7684\u5206\u9694\u64cd\u4f5c\uff0c<code>textwrap<\/code> \u6a21\u5757\u9002\u5408\u683c\u5f0f\u5316\u6587\u672c\u6bb5\u843d\uff0c\u751f\u6210\u5668\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u91cf\u7684\u5b57\u7b26\u4e32\u5206\u5272\uff0c<code>partition()<\/code> \u548c <code>rpartition()<\/code> \u65b9\u6cd5\u9002\u5408\u4e00\u6b21\u6027\u5206\u9694\u64cd\u4f5c\uff0c<code>splitlines()<\/code> \u65b9\u6cd5\u9002\u5408\u5904\u7406\u591a\u884c\u6587\u672c\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u7075\u6d3b\u8fd0\u7528Python\u8fdb\u884c\u5b57\u7b26\u4e32\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u7279\u5b9a\u7684\u884c\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u548c\u6362\u884c\u7b26\u6765\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u7279\u5b9a\u7684\u884c\u6570\u3002\u60a8\u53ef\u4ee5\u8ba1\u7b97\u6bcf\u884c\u7684\u5b57\u7b26\u6570\uff0c\u5e76\u4f7f\u7528\u5207\u7247\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u884c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">text = &quot;\u8fd9\u662f\u4e00\u4e2a\u9700\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u3002&quot;\nline_length = 10  # \u6bcf\u884c\u7684\u5b57\u7b26\u6570\nlines = [text[i:i + line_length] for i in range(0, len(text), line_length)]\nfor line in lines:\n    print(line)\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u63a7\u5236\u5b57\u7b26\u4e32\u7684\u6362\u884c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u7b80\u5355\u7684\u5207\u7247\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\u6362\u884c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>textwrap<\/code>\u6a21\u5757\u4e2d\u7684<code>fill()<\/code>\u51fd\u6570\u6765\u81ea\u52a8\u5904\u7406\u6362\u884c\uff0c\u786e\u4fdd\u6bcf\u884c\u7684\u5bbd\u5ea6\u4e0d\u8d85\u8fc7\u6307\u5b9a\u7684\u5b57\u7b26\u6570\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import textwrap\ntext = &quot;\u8fd9\u662f\u4e00\u4e2a\u9700\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u3002&quot;\nwrapped_text = textwrap.fill(text, width=10)\nprint(wrapped_text)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u957f\u6587\u672c\uff0c\u80fd\u591f\u81ea\u52a8\u5728\u9002\u5f53\u7684\u5730\u65b9\u63d2\u5165\u6362\u884c\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u957f\u5b57\u7b26\u4e32\u5e76\u786e\u4fdd\u4e0d\u5728\u5355\u8bcd\u4e2d\u95f4\u65ad\u5f00\uff1f<\/strong><br \/>\u5728\u5206\u5272\u5b57\u7b26\u4e32\u65f6\uff0c\u907f\u514d\u5728\u5355\u8bcd\u4e2d\u95f4\u65ad\u5f00\u7684\u9700\u6c42\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>textwrap<\/code>\u6a21\u5757\u7684<code>wrap()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u6b64\u51fd\u6570\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u5bbd\u5ea6\u81ea\u52a8\u5206\u884c\uff0c\u5e76\u786e\u4fdd\u6bcf\u884c\u5b8c\u6574\u7684\u5355\u8bcd\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import textwrap\ntext = &quot;\u8fd9\u662f\u4e00\u4e2a\u9700\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u4e0d\u4f1a\u5728\u5355\u8bcd\u4e2d\u95f4\u65ad\u5f00\u3002&quot;\nwrapped_lines = textwrap.wrap(text, width=10)\nfor line in wrapped_lines:\n    print(line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6bcf\u884c\u7684\u6587\u672c\u7f8e\u89c2\u4e14\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u884c\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4ee5\u53ca\u6587\u672c\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":1141296,"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\/1141292"}],"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=1141292"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141292\/revisions"}],"predecessor-version":[{"id":1141299,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141292\/revisions\/1141299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}