{"id":944524,"date":"2024-12-26T23:08:14","date_gmt":"2024-12-26T15:08:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/944524.html"},"modified":"2024-12-26T23:08:16","modified_gmt":"2024-12-26T15:08:16","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80-str","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/944524.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00 str"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081452\/7f288252-763d-4585-a94d-32159884a748.webp\" alt=\"python\u5982\u4f55\u6253\u5f00 str\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u6253\u5f00\u5b57\u7b26\u4e32\uff08str\uff09\u8fdb\u884c\u5904\u7406\uff0c\u5305\u62ec\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6587\u4ef6I\/O\u64cd\u4f5c\u3001\u548c\u6570\u636e\u89e3\u6790\u7b49\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5339\u914d\u548c\u66ff\u6362\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\u662fPython\u4e2d\u6700\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3aPython\u5185\u7f6e\u4e86\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u627e\u3001\u5206\u5272\u3001\u8fde\u63a5\u548c\u4fee\u6539\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5217\u8868\uff0c\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u91cd\u65b0\u7ec4\u5408\u6210\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u7075\u6d3b\u64cd\u4f5c\u80fd\u529b\uff0c\u800c\u4e0d\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6267\u884c\u5404\u79cd\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u5982\u62c6\u5206\u3001\u8fde\u63a5\u3001\u66ff\u6362\u3001\u67e5\u627e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b57\u7b26\u4e32\u62c6\u5206\u548c\u8fde\u63a5<\/h4>\n<\/p>\n<ul>\n<li>\n<p><strong>\u62c6\u5206<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is easy to learn&quot;<\/p>\n<p>words = text.split()  # \u9ed8\u8ba4\u6309\u7167\u7a7a\u683c\u62c6\u5206<\/p>\n<p>print(words)  # \u8f93\u51fa: [&#39;Python&#39;, &#39;is&#39;, &#39;easy&#39;, &#39;to&#39;, &#39;learn&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u7684\u5143\u7d20\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = [&#39;Python&#39;, &#39;is&#39;, &#39;easy&#39;, &#39;to&#39;, &#39;learn&#39;]<\/p>\n<p>sentence = &#39; &#39;.join(words)<\/p>\n<p>print(sentence)  # \u8f93\u51fa: Python is easy to learn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>1.2 \u5b57\u7b26\u4e32\u67e5\u627e\u548c\u66ff\u6362<\/h4>\n<\/p>\n<ul>\n<li>\n<p><strong>\u67e5\u627e<\/strong>\uff1a<code>find()<\/code>\u548c<code>index()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, world!&quot;<\/p>\n<p>position = text.find(&quot;world&quot;)<\/p>\n<p>print(position)  # \u8f93\u51fa: 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66ff\u6362<\/strong>\uff1a<code>replace()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/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<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\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\u6765\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5339\u914d\u6a21\u5f0f<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>match()<\/code>\u548c<code>search()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6a21\u5f0f\u3002<\/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>result = re.search(r&quot;\\bS\\w+&quot;, text)<\/p>\n<p>print(result.group())  # \u8f93\u51fa: Spain<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2.2 \u66ff\u6362\u6a21\u5f0f<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>sub()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;The rain in Spain&quot;<\/p>\n<p>result = re.sub(r&quot;\\bS\\w+&quot;, &quot;cloud&quot;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa: The rain in cloud<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u6587\u4ef6I\/O\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>3.2 \u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6570\u636e\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u51fa\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528<code>json<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<ul>\n<li>\n<p><code>json<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790\u548c\u751f\u6210JSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_string = &#39;{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}&#39;<\/p>\n<p>data = json.loads(json_string)<\/p>\n<p>print(data[&#39;name&#39;])  # \u8f93\u51fa: John<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>4.2 \u4f7f\u7528<code>csv<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<ul>\n<li>\n<p><code>csv<\/code>\u6a21\u5757\u7528\u4e8e\u89e3\u6790CSV\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.reader(csvfile)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528<code>%<\/code>\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684printf\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>print(&quot;Name: %s, Age: %d&quot; % (name, age))  # \u8f93\u51fa: Name: Alice, Age: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>5.2 \u4f7f\u7528<code>str.format()<\/code><\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u8fd9\u662fPython\u8f83\u65b0\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Bob&quot;<\/p>\n<p>age = 30<\/p>\n<p>print(&quot;Name: {}, Age: {}&quot;.format(name, age))  # \u8f93\u51fa: Name: Bob, Age: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>5.3 \u4f7f\u7528f-string\uff08Python 3.6+\uff09<\/h4>\n<\/p>\n<ul>\n<li>\n<p>f-string\u662fPython 3.6\u4e2d\u5f15\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Charlie&quot;<\/p>\n<p>age = 35<\/p>\n<p>print(f&quot;Name: {name}, Age: {age}&quot;)  # \u8f93\u51fa: Name: Charlie, Age: 35<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u5b57\u7b26\u4e32\u7f16\u7801\u548c\u89e3\u7801<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u9700\u8981\u4e86\u89e3\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u7f16\u7801\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>encode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u5b57\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, world!&quot;<\/p>\n<p>byte_data = text.encode(&#39;utf-8&#39;)<\/p>\n<p>print(byte_data)  # \u8f93\u51fa: b&#39;Hello, world!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>6.2 \u89e3\u7801\u5b57\u8282<\/h4>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>decode()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">byte_data = b&#39;Hello, world!&#39;<\/p>\n<p>text = byte_data.decode(&#39;utf-8&#39;)<\/p>\n<p>print(text)  # \u8f93\u51fa: Hello, world!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e0d\u540c\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u7075\u6d3b\u6027\u548c\u591a\u6837\u6027\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e0b\u6709\u6548\u5730\u64cd\u4f5c\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u89e3\u6790\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\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\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>exec()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u4ee3\u7801\u6267\u884c\u3002\u901a\u8fc7\u5c06\u5305\u542b\u6709\u6548Python\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9<code>exec()<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u4ee3\u7801\u6267\u884c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">code = &quot;print(&#39;Hello, World!&#39;)&quot;\nexec(code)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa<code>Hello, World!<\/code>\u3002\u4e0d\u8fc7\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>exec()<\/code>\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff0c\u56e0\u4e3a\u6267\u884c\u7684\u4ee3\u7801\u53ef\u80fd\u5305\u542b\u6076\u610f\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u548c\u64cd\u4f5c\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002\u5c06\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u6307\u5b9a\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u8bfb\u53d6\uff09\u3002\u8bfb\u53d6\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u67e5\u627e\u3001\u66ff\u6362\u7b49\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5e38\u89c1\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5982<code>strip()<\/code>\u3001<code>split()<\/code>\u3001<code>join()<\/code>\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6e05\u7406\u3001\u5206\u5272\u6216\u8fde\u63a5\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot; Hello, World! &quot;\ncleaned_text = text.strip()  # \u53bb\u9664\u9996\u5c3e\u7a7a\u683c\nwords = cleaned_text.split(&#39;, &#39;)  # \u6309\u9017\u53f7\u5206\u5272\nprint(words)  # \u8f93\u51fa: [&#39;Hello&#39;, &#39;World!&#39;]\n<\/code><\/pre>\n<p>\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u6709\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u6253\u5f00\u5b57\u7b26\u4e32\uff08str\uff09\u8fdb\u884c\u5904\u7406\uff0c\u5305\u62ec\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6587\u4ef6I\/O\u64cd\u4f5c\u3001\u548c\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":944527,"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\/944524"}],"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=944524"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944524\/revisions"}],"predecessor-version":[{"id":944530,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/944524\/revisions\/944530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/944527"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=944524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=944524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=944524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}