{"id":1150798,"date":"2025-01-13T17:05:52","date_gmt":"2025-01-13T09:05:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150798.html"},"modified":"2025-01-13T17:05:54","modified_gmt":"2025-01-13T09:05:54","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%ad%97%e7%ac%a6%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150798.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5b57\u7b26\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181216\/afcad9a5-73bf-4869-a9f4-d7a92fc35df9.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5b57\u7b26\u8fd0\u7b97\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u5b57\u7b26\u8fd0\u7b97\u7684\u65b9\u6cd5\u6709\uff1a\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5b57\u7b26\u4e32\u67e5\u627e\u4e0e\u66ff\u6362\u3001\u5b57\u7b26\u4e32\u5206\u5272\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\u8fde\u63a5<\/strong>\u662f\u6700\u57fa\u7840\u7684\u5b57\u7b26\u8fd0\u7b97\u4e4b\u4e00\uff0c\u901a\u5e38\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 &quot;Hello&quot; \u548c &quot;World&quot; \u53ef\u4ee5\u901a\u8fc7 &quot;Hello&quot; + &quot;World&quot; \u8fde\u63a5\u6210 &quot;HelloWorld&quot;\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 join() \u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b57\u7b26\u4e32\u8fde\u63a5<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u8fde\u63a5\u662f\u6700\u5e38\u89c1\u7684\u5b57\u7b26\u8fd0\u7b97\u4e4b\u4e00\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u6216 join() \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u52a0\u53f7\uff08+\uff09<\/h2>\n<\/p>\n<p><p>\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;World&quot;<\/p>\n<p>result = str1 + &quot; &quot; + str2<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u5bf9\u4e8e\u5927\u91cf\u5b57\u7b26\u4e32\u7684\u8fde\u63a5\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 join() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>join() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\uff0c\u6548\u7387\u8f83\u9ad8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;Hello&quot;, &quot;World&quot;]<\/p>\n<p>result = &quot; &quot;.join(str_list)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello World<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fde\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u4ee5\u901a\u8fc7\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u6765\u751f\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709 % \u64cd\u4f5c\u7b26\u3001format() \u65b9\u6cd5\u548c f-strings\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 % \u64cd\u4f5c\u7b26<\/h2>\n<\/p>\n<p><p>% \u64cd\u4f5c\u7b26\u53ef\u4ee5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u7684\u5360\u4f4d\u7b26\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>result = &quot;My name is %s and I am %d years old.&quot; % (name, age)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aMy name is Alice and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528 format() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>format() \u65b9\u6cd5\u901a\u8fc7\u5927\u62ec\u53f7 {} \u4f5c\u4e3a\u5360\u4f4d\u7b26\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u672b\u5c3e\u8c03\u7528 format() \u65b9\u6cd5\u4f20\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>result = &quot;My name is {} and I am {} years old.&quot;.format(name, age)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aMy name is Alice and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528 f-strings<\/h2>\n<\/p>\n<p><p>f-strings \u662f Python 3.6 \u5f15\u5165\u7684\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0\u5b57\u6bcd f\uff0c\u5e76\u5728\u5927\u62ec\u53f7 {} \u4e2d\u63d2\u5165\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>age = 25<\/p>\n<p>result = f&quot;My name is {name} and I am {age} years old.&quot;<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aMy name is Alice and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5b57\u7b26\u4e32\u67e5\u627e\u4e0e\u66ff\u6362<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u67e5\u627e\u4e0e\u66ff\u6362\u662f\u5b57\u7b26\u8fd0\u7b97\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528 find() \u65b9\u6cd5\u548c replace() \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 find() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>find() \u65b9\u6cd5\u53ef\u4ee5\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de -1\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello, World!&quot;<\/p>\n<p>index = str1.find(&quot;World&quot;)<\/p>\n<p>print(index)  # \u8f93\u51fa\uff1a7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528 replace() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>replace() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u65b0\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello, World!&quot;<\/p>\n<p>result = str1.replace(&quot;World&quot;, &quot;Python&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aHello, Python!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5b57\u7b26\u4e32\u5206\u5272<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u5206\u5272\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u662f split()\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 split() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>split() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u6210\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;apple,banana,cherry&quot;<\/p>\n<p>result = str1.split(&quot;,&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u672a\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u5219\u9ed8\u8ba4\u6309\u7a7a\u683c\u5206\u5272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello World&quot;<\/p>\n<p>result = str1.split()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5b57\u7b26\u4e32\u62fc\u63a5<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u5c06\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u662f join()\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 join() \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>join() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e8f\u5217\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62fc\u63a5\u6210\u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<p>result = &quot;, &quot;.join(str_list)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aapple, banana, cherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5b57\u7b26\u4e32\u6bd4\u8f83<\/strong><\/h2>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u6216\u8005\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08==, !=, &lt;, &gt;, &lt;=, &gt;=\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/h2>\n<\/p>\n<p><p>\u6bd4\u8f83\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;apple&quot;<\/p>\n<p>str2 = &quot;banana&quot;<\/p>\n<p>print(str1 == str2)  # \u8f93\u51fa\uff1aFalse<\/p>\n<p>print(str1 != str2)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(str1 &lt; str2)   # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(str1 &gt; str2)   # \u8f93\u51fa\uff1aFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u662f\u57fa\u4e8e\u5b57\u5178\u987a\u5e8f\u8fdb\u884c\u7684\uff0c\u5b57\u6bcd\u8868\u987a\u5e8f\u9760\u524d\u7684\u5b57\u7b26\u4e32\u8f83\u5c0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u5904\u7406\u7528\u6237\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u9a8c\u8bc1\u3001\u683c\u5f0f\u5316\u548c\u63d0\u53d6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Enter your em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l: &quot;)<\/p>\n<p>if &quot;@&quot; in user_input and &quot;.&quot; in user_input:<\/p>\n<p>    username, domain = user_input.split(&quot;@&quot;)<\/p>\n<p>    print(f&quot;Username: {username}&quot;)<\/p>\n<p>    print(f&quot;Domain: {domain}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid email address&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u751f\u6210\u52a8\u6001\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u52a8\u6001\u5185\u5bb9\u65f6\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u548c\u62fc\u63a5\u662f\u5e38\u7528\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">template = &quot;Hello, {name}! You have {count} new messages.&quot;<\/p>\n<p>name = &quot;Alice&quot;<\/p>\n<p>count = 5<\/p>\n<p>message = template.format(name=name, count=count)<\/p>\n<p>print(message)  # \u8f93\u51fa\uff1aHello, Alice! You have 5 new messages.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6570\u636e\u89e3\u6790\u4e0e\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u89e3\u6790\u548c\u8f6c\u6362\u6570\u636e\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u3001\u66ff\u6362\u548c\u62fc\u63a5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = &quot;name:Alice,age:25,city:New York&quot;<\/p>\n<p>info = {}<\/p>\n<p>for item in data.split(&quot;,&quot;):<\/p>\n<p>    key, value = item.split(&quot;:&quot;)<\/p>\n<p>    info[key] = value<\/p>\n<p>print(info)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: &#39;25&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u9700\u8981\u5173\u6ce8\u7684\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528 join() \u4ee3\u66ff +<\/h2>\n<\/p>\n<p><p>\u5728\u62fc\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528 join() \u65b9\u6cd5\u6bd4\u4f7f\u7528\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u6548\u7387\u66f4\u9ad8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;Hello&quot;] * 1000<\/p>\n<p>result = &quot;&quot;.join(str_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u8282\u7701\u5185\u5b58\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_strings():<\/p>\n<p>    for i in range(1000):<\/p>\n<p>        yield f&quot;String {i}&quot;<\/p>\n<p>result = &quot;\\n&quot;.join(generate_strings())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u590d\u5236<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u590d\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>str1 = &quot;Hello&quot;<\/p>\n<p>str2 = str1 + &quot; World&quot;<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>str1 = &quot;Hello&quot;<\/p>\n<p>str2 = f&quot;{str1} World&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5b57\u7b26\u4e32\u5904\u7406\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u591a\u884c\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&quot;&quot;&quot; \u6216 &#39;&#39;&#39;\uff09\u6765\u5b9a\u4e49\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">multiline_str = &quot;&quot;&quot;Hello,<\/p>\n<p>World!<\/p>\n<p>This is a multiline string.&quot;&quot;&quot;<\/p>\n<p>print(multiline_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 strip()\u3001lstrip() \u548c rstrip() \u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;  Hello, World!  &quot;<\/p>\n<p>print(str1.strip())   # \u8f93\u51fa\uff1aHello, World!<\/p>\n<p>print(str1.lstrip())  # \u8f93\u51fa\uff1aHello, World!  <\/p>\n<p>print(str1.rstrip())  # \u8f93\u51fa\uff1a  Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\uff08\\\uff09\u6216\u8005 raw \u5b57\u7b26\u4e32\uff08\u5728\u5b57\u7b26\u4e32\u524d\u52a0 r\uff09\u6765\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello\\nWorld!&quot;<\/p>\n<p>print(str1)  # \u8f93\u51fa\uff1a<\/p>\n<p>             # Hello<\/p>\n<p>             # World!<\/p>\n<p>str2 = r&quot;Hello\\nWorld!&quot;<\/p>\n<p>print(str2)  # \u8f93\u51fa\uff1aHello\\nWorld!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u9ad8\u7ea7\u5e94\u7528<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5b57\u7b26\u4e32\u5904\u7406\u9700\u8981\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5176\u4ed6\u9ad8\u7ea7\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u4f7f\u7528 re \u6a21\u5757\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\b\\w{5}\\b&quot;<\/p>\n<p>text = &quot;Hello World! This is a Python tutorial.&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528\u6a21\u677f\u5f15\u64ce<\/h2>\n<\/p>\n<p><p>\u5728\u751f\u6210\u52a8\u6001\u5185\u5bb9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u6765\u7b80\u5316\u5b57\u7b26\u4e32\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from jinja2 import Template<\/p>\n<p>template = Template(&quot;Hello, {{ name }}!&quot;)<\/p>\n<p>message = template.render(name=&quot;Alice&quot;)<\/p>\n<p>print(message)  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u5e38\u7528\u5e93<\/strong><\/h2>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u5904\u7406\u5e93\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u7528\u5e93\u7684\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h2>re \u6a21\u5757<\/h2>\n<\/p>\n<p><p>re \u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\uff0c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\b\\w{5}\\b&quot;<\/p>\n<p>text = &quot;Hello World! This is a Python tutorial.&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>string \u6a21\u5757<\/h2>\n<\/p>\n<p><p>string \u6a21\u5757\u63d0\u4f9b\u4e86\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u7b26\u96c6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import string<\/p>\n<p>print(string.ascii_letters)  # \u8f93\u51fa\uff1aabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<\/p>\n<p>print(string.digits)         # \u8f93\u51fa\uff1a0123456789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>textwrap \u6a21\u5757<\/h2>\n<\/p>\n<p><p>textwrap \u6a21\u5757\u63d0\u4f9b\u4e86\u6587\u672c\u6362\u884c\u548c\u586b\u5145\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import textwrap<\/p>\n<p>text = &quot;This is a very long text that needs to be wrapped.&quot;<\/p>\n<p>wrapped_text = textwrap.fill(text, width=20)<\/p>\n<p>print(wrapped_text)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>This is a very long<\/strong><\/h2>\n<h2><strong>text that needs to<\/strong><\/h2>\n<h2><strong>be wrapped.<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>difflib \u6a21\u5757<\/h2>\n<\/p>\n<p><p>difflib \u6a21\u5757\u63d0\u4f9b\u4e86\u5b57\u7b26\u4e32\u5dee\u5f02\u6bd4\u8f83\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import difflib<\/p>\n<p>str1 = &quot;Hello, World!&quot;<\/p>\n<p>str2 = &quot;Hello, Python!&quot;<\/p>\n<p>diff = difflib.ndiff(str1, str2)<\/p>\n<p>print(&#39;\\n&#39;.join(diff))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e8c\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9075\u5faa\u5b57\u7b26\u4e32\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u62fc\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\uff0c\u4f18\u5148\u9009\u62e9 join() \u65b9\u6cd5\u800c\u4e0d\u662f\u52a0\u53f7\uff08+\uff09\u8fd0\u7b97\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u907f\u514d\u786c\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u53d8\u91cf\u6216\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u63a8\u8350<\/p>\n<p>print(&quot;Hello, World!&quot;)<\/p>\n<h2><strong>\u63a8\u8350<\/strong><\/h2>\n<p>greeting = &quot;Hello, World!&quot;<\/p>\n<p>print(greeting)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528\u6ce8\u91ca\u548c\u6587\u6863<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u65f6\uff0c\u6dfb\u52a0\u6ce8\u91ca\u548c\u6587\u6863\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5339\u914d\u6240\u6709\u5305\u542b5\u4e2a\u5b57\u7b26\u7684\u5355\u8bcd<\/strong><\/h2>\n<p>pattern = r&quot;\\b\\w{5}\\b&quot;<\/p>\n<p>text = &quot;Hello World! This is a Python tutorial.&quot;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa\uff1a[&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u5904\u7406\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class TestStringMethods(unittest.TestCase):<\/p>\n<p>    def test_upper(self):<\/p>\n<p>        self.assertEqual(&#39;foo&#39;.upper(), &#39;FOO&#39;)<\/p>\n<p>    def test_isupper(self):<\/p>\n<p>        self.assertTrue(&#39;FOO&#39;.isupper())<\/p>\n<p>        self.assertFalse(&#39;Foo&#39;.isupper())<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u4e86\u89e3Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u8fd0\u7b97\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u4e0e\u5206\u5272\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08<code>+<\/code>\uff09\u8fd0\u7b97\u7b26\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>str1 = &quot;Hello&quot;<\/code> \u548c <code>str2 = &quot;World&quot;<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>result = str1 + &quot; &quot; + str2<\/code> \u5f97\u5230 <code>&quot;Hello World&quot;<\/code>\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\u7684\u5206\u5272\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5207\u5272\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u4f8b\u5982 <code>text = &quot;apple,banana,cherry&quot;<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>text.split(&quot;,&quot;)<\/code> \u5f97\u5230 <code>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/code> \u7684\u5217\u8868\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u67e5\u627e\u548c\u66ff\u6362\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528 <code>find()<\/code> \u6216 <code>index()<\/code> \u65b9\u6cd5\uff0c\u524d\u8005\u5728\u627e\u4e0d\u5230\u65f6\u8fd4\u56de <code>-1<\/code>\uff0c\u540e\u8005\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u66ff\u6362\u5b57\u7b26\u4e32\u5219\u53ef\u4ee5\u4f7f\u7528 <code>replace()<\/code> \u65b9\u6cd5\uff0c\u4f8b\u5982 <code>text = &quot;I like apples&quot;<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>text.replace(&quot;apples&quot;, &quot;oranges&quot;)<\/code> \u5c06\u5176\u53d8\u4e3a <code>&quot;I like oranges&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5728Python\u4e2d\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>f-string<\/code>\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\uff0c\u4f8b\u5982 <code>name = &quot;Alice&quot;<\/code> \u548c <code>age = 30<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>f&quot;My name is {name} and I am {age} years old.&quot;<\/code> \u6765\u751f\u6210 <code>&quot;My name is Alice and I am 30 years old.&quot;<\/code>\u3002\u53e6\u5916\uff0c<code>format()<\/code> \u65b9\u6cd5\u548c <code>%<\/code> \u64cd\u4f5c\u7b26\u4e5f\u662f\u5e38\u7528\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u5b57\u7b26\u8fd0\u7b97\u7684\u65b9\u6cd5\u6709\uff1a\u5b57\u7b26\u4e32\u8fde\u63a5\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5b57\u7b26\u4e32\u67e5\u627e\u4e0e\u66ff\u6362\u3001\u5b57\u7b26\u4e32\u5206\u5272\u3001\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u5b57\u7b26\u4e32\u6bd4 [&hellip;]","protected":false},"author":3,"featured_media":1150806,"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\/1150798"}],"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=1150798"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150798\/revisions"}],"predecessor-version":[{"id":1150808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150798\/revisions\/1150808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150806"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}