{"id":1025674,"date":"2024-12-31T10:36:02","date_gmt":"2024-12-31T02:36:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025674.html"},"modified":"2024-12-31T10:36:05","modified_gmt":"2024-12-31T02:36:05","slug":"%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e5%80%bcpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025674.html","title":{"rendered":"\u5982\u4f55\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u503cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4c5039e4-7cf7-4afa-9547-3ecdf8bdc71e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u503cpython\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u3001\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/strong>\u6700\u5e38\u7528\u7684\u6bd4\u8f83\u65b9\u6cd5\u662f\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u5982==\u3001!=\u3001&lt;\u3001&gt;\u3001&lt;=\u3001&gt;=\uff09\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5982<code>ord()<\/code>\u548c<code>cmp()<\/code>\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u3002<strong>\u5b57\u7b26\u4e32\u6bd4\u8f83\u5728Python\u4e2d\u662f\u57fa\u4e8eUnicode\u7801\u70b9\u7684\u987a\u5e8f\u8fdb\u884c\u7684<\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u7b26\u7684\u6bd4\u8f83\u662f\u57fa\u4e8e\u5b83\u4eec\u7684ASCII\u6216Unicode\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u503c\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b49\u4e8e\uff08==\uff09\u548c\u4e0d\u7b49\u4e8e\uff08!=\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u7b49\u4e8e\u8fd0\u7b97\u7b26\uff08==\uff09\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u4e0d\u7b49\u4e8e\u8fd0\u7b97\u7b26\uff08!=\uff09\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e0d\u76f8\u7b49\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>str3 = &quot;hello&quot;<\/p>\n<p>print(str1 == str2)  # False<\/p>\n<p>print(str1 == str3)  # True<\/p>\n<p>print(str1 != str2)  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>str1<\/code>\u548c<code>str3<\/code>\u76f8\u7b49\uff0c\u6240\u4ee5<code>str1 == str3<\/code>\u8fd4\u56de<code>True<\/code>\u3002\u800c<code>str1<\/code>\u548c<code>str2<\/code>\u4e0d\u76f8\u7b49\uff0c\u6240\u4ee5<code>str1 != str2<\/code>\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5927\u4e8e\uff08&gt;\uff09\u3001\u5c0f\u4e8e\uff08&lt;\uff09\u3001\u5927\u4e8e\u7b49\u4e8e\uff08&gt;=\uff09\u548c\u5c0f\u4e8e\u7b49\u4e8e\uff08&lt;=\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u5b57\u5178\u987a\u5e8f\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 &lt; str2)   # True<\/p>\n<p>print(str1 &gt; str2)   # False<\/p>\n<p>print(str1 &lt;= str2)  # True<\/p>\n<p>print(str1 &gt;= str2)  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>str1<\/code>\u5728\u5b57\u5178\u987a\u5e8f\u4e2d\u5c0f\u4e8e<code>str2<\/code>\uff0c\u6240\u4ee5<code>str1 &lt; str2<\/code>\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6bd4\u8f83\u8fd0\u7b97\u7b26\u4e4b\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>ord()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>ord()<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u5355\u4e2a\u5b57\u7b26\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char1 = &#39;a&#39;<\/p>\n<p>char2 = &#39;b&#39;<\/p>\n<p>print(ord(char1))  # 97<\/p>\n<p>print(ord(char2))  # 98<\/p>\n<p>print(ord(char1) &lt; ord(char2))  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>ord(char1)<\/code>\u8fd4\u56de\u5b57\u7b26<code>&#39;a&#39;<\/code>\u7684Unicode\u7801\u70b997\uff0c\u800c<code>ord(char2)<\/code>\u8fd4\u56de\u5b57\u7b26<code>&#39;b&#39;<\/code>\u7684Unicode\u7801\u70b998\uff0c\u6240\u4ee5<code>ord(char1) &lt; ord(char2)<\/code>\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>str.casefold()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>str.casefold()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199\u5f62\u5f0f\uff0c\u4ece\u800c\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str1 = &quot;Hello&quot;<\/p>\n<p>str2 = &quot;hello&quot;<\/p>\n<p>print(str1.casefold() == str2.casefold())  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>str1.casefold()<\/code>\u548c<code>str2.casefold()<\/code>\u90fd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5c0f\u5199<code>&quot;hello&quot;<\/code>\uff0c\u6240\u4ee5\u7ed3\u679c\u4e3a<code>True<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u903b\u8f91\uff0c\u8fd9\u65f6\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6bd4\u8f83\u5b57\u7b26\u4e32\u957f\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compare_length(str1, str2):<\/p>\n<p>    if len(str1) &lt; len(str2):<\/p>\n<p>        return -1<\/p>\n<p>    elif len(str1) &gt; len(str2):<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return 0<\/p>\n<p>str1 = &quot;hello&quot;<\/p>\n<p>str2 = &quot;worlds&quot;<\/p>\n<p>print(compare_length(str1, str2))  # -1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>compare_length<\/code>\u51fd\u6570\u8fd4\u56de<code>-1<\/code>\uff0c\u8868\u793a<code>str1<\/code>\u7684\u957f\u5ea6\u5c0f\u4e8e<code>str2<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5ffd\u7565\u7a7a\u683c\u548c\u5927\u5c0f\u5199\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5ffd\u7565\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u548c\u5927\u5c0f\u5199\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compare_ignore_whitespace_case(str1, str2):<\/p>\n<p>    str1 = str1.replace(&quot; &quot;, &quot;&quot;).casefold()<\/p>\n<p>    str2 = str2.replace(&quot; &quot;, &quot;&quot;).casefold()<\/p>\n<p>    if str1 &lt; str2:<\/p>\n<p>        return -1<\/p>\n<p>    elif str1 &gt; str2:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return 0<\/p>\n<p>str1 = &quot;Hello World&quot;<\/p>\n<p>str2 = &quot;hello  world&quot;<\/p>\n<p>print(compare_ignore_whitespace_case(str1, str2))  # 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>compare_ignore_whitespace_case<\/code>\u51fd\u6570\u5ffd\u7565\u7a7a\u683c\u548c\u5927\u5c0f\u5199\u8fdb\u884c\u6bd4\u8f83\uff0c\u7ed3\u679c\u4e3a<code>0<\/code>\uff0c\u8868\u793a<code>str1<\/code>\u548c<code>str2<\/code>\u76f8\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b57\u7b26\u4e32\u6bd4\u8f83\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6392\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\u975e\u5e38\u91cd\u8981\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c<code>list.sort()<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">strings = [&quot;banana&quot;, &quot;apple&quot;, &quot;cherry&quot;]<\/p>\n<p>sorted_strings = sorted(strings)<\/p>\n<p>print(sorted_strings)  # [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>strings.sort()<\/p>\n<p>print(strings)  # [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>sorted()<\/code>\u51fd\u6570\u548c<code>list.sort()<\/code>\u65b9\u6cd5\u90fd\u5bf9\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u4e86\u5b57\u5178\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u627e\u548c\u8fc7\u6ee4<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u4e5f\u5e38\u7528\u4e8e\u67e5\u627e\u548c\u8fc7\u6ee4\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>startswith()<\/code>\u548c<code>endswith()<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u4ee5\u7279\u5b9a\u524d\u7f00\u6216\u540e\u7f00\u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">strings = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;apricot&quot;]<\/p>\n<p>result = [s for s in strings if s.startswith(&quot;ap&quot;)]<\/p>\n<p>print(result)  # [&#39;apple&#39;, &#39;apricot&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>startswith()<\/code>\u65b9\u6cd5\u627e\u5230\u4e86\u4ee5<code>&quot;ap&quot;<\/code>\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u53bb\u91cd<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u96c6\u5408\u65f6\uff0c\u53bb\u91cd\u64cd\u4f5c\u4e5f\u975e\u5e38\u5e38\u89c1\u3002\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u53bb\u91cd\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">strings = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;apple&quot;, &quot;banana&quot;]<\/p>\n<p>unique_strings = list(set(strings))<\/p>\n<p>print(unique_strings)  # [&#39;cherry&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>set<\/code>\u53bb\u9664\u4e86\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u503c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<strong>\u6bd4\u8f83\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u7528\u7684\u7b80\u5355\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6bd4\u8f83\u573a\u666f\u3002<strong>\u5185\u7f6e\u51fd\u6570\u5982<code>ord()<\/code>\u548c<code>casefold()<\/code>\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u9700\u6c42<\/strong>\u3002<strong>\u81ea\u5b9a\u4e49\u51fd\u6570\u5219\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5b9e\u73b0\u5b9a\u5236\u5316\u7684\u6bd4\u8f83\u903b\u8f91<\/strong>\u3002\u6b64\u5916\uff0c\u5b57\u7b26\u4e32\u6bd4\u8f83\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4f8b\u5982\u6392\u5e8f\u3001\u67e5\u627e\u3001\u8fc7\u6ee4\u548c\u53bb\u91cd\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982 <code>&lt;<\/code>, <code>&gt;<\/code>, <code>==<\/code> \u7b49\u3002\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u57fa\u4e8e\u5b57\u5178\u5e8f\u8fdb\u884c\uff0c\u5177\u4f53\u6765\u8bf4\uff0cPython\u4f1a\u9010\u4e2a\u5b57\u7b26\u8fdb\u884c\u6bd4\u8f83\uff0c\u76f4\u5230\u9047\u5230\u4e0d\u540c\u7684\u5b57\u7b26\u4e3a\u6b62\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u5728\u5b57\u5178\u5e8f\u4e2d\u6392\u5728\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e4b\u524d\uff0c\u5219\u524d\u8005\u5c0f\u4e8e\u540e\u8005\u3002\u4f8b\u5982\uff0c<code>&quot;apple&quot; &lt; &quot;banana&quot;<\/code> \u4e3a\u771f\uff0c\u56e0\u4e3a\u5b57\u6bcd&quot;a&quot;\u5728\u5b57\u6bcd&quot;b&quot;\u4e4b\u524d\u3002<\/p>\n<p><strong>\u6bd4\u8f83\u5b57\u7b26\u4e32\u65f6\uff0c\u533a\u5206\u5927\u5c0f\u5199\u4f1a\u5f71\u54cd\u7ed3\u679c\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5728\u6bd4\u8f83\u5b57\u7b26\u4e32\u65f6\u4f1a\u8003\u8651\u5b57\u7b26\u7684ASCII\u503c\uff0c\u56e0\u6b64\u5927\u5199\u5b57\u6bcd\u7684ASCII\u503c\u5c0f\u4e8e\u5c0f\u5199\u5b57\u6bcd\u3002\u4f8b\u5982\uff0c<code>&quot;Apple&quot; &lt; &quot;apple&quot;<\/code> \u4e3a\u771f\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u6bd4\u8f83\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7edf\u4e00\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\uff0c\u4f7f\u7528 <code>str.lower()<\/code> \u6216 <code>str.upper()<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u5ffd\u7565\u7a7a\u683c\u548c\u5927\u5c0f\u5199\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u4e3a\u4e86\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u800c\u5ffd\u7565\u7a7a\u683c\u548c\u5927\u5c0f\u5199\u7684\u5dee\u5f02\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>str.strip()<\/code> \u65b9\u6cd5\u53bb\u6389\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u683c\uff0c\u5e76\u7ed3\u5408 <code>str.lower()<\/code> \u6216 <code>str.upper()<\/code> \u6765\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">str1 = &quot; Hello &quot;\nstr2 = &quot;hello&quot;\nis_equal = str1.strip().lower() == str2.lower()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>is_equal<\/code> \u5c06\u4f1a\u8fd4\u56de <code>True<\/code>\uff0c\u5373\u4f7f\u4e24\u4e2a\u5b57\u7b26\u4e32\u5728\u539f\u59cb\u72b6\u6001\u4e0b\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u3001\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u6700\u5e38\u7528\u7684\u6bd4\u8f83\u65b9\u6cd5\u662f\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1025685,"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\/1025674"}],"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=1025674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025674\/revisions"}],"predecessor-version":[{"id":1025688,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025674\/revisions\/1025688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}