{"id":1179573,"date":"2025-01-15T18:25:29","date_gmt":"2025-01-15T10:25:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1179573.html"},"modified":"2025-01-15T18:25:31","modified_gmt":"2025-01-15T10:25:31","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e5%ae%9a%e5%a4%a7%e5%b0%8f%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1179573.html","title":{"rendered":"python\u5982\u4f55\u5224\u5b9a\u5927\u5c0f\u5199"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113942\/ba3389fe-de8d-44f9-a295-45191eec61b6.webp\" alt=\"python\u5982\u4f55\u5224\u5b9a\u5927\u5c0f\u5199\" \/><\/p>\n<p><p> <strong>Python\u5224\u5b9a\u5927\u5c0f\u5199\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>isupper()<\/code>\u65b9\u6cd5\u3001<code>islower()<\/code>\u65b9\u6cd5\u3001<code>isalpha()<\/code>\u65b9\u6cd5\u3001<code>upper()<\/code>\u65b9\u6cd5\u548c<code>lower()<\/code>\u65b9\u6cd5\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8be6\u7ec6\u8ba8\u8bba\u4e00\u4e0b<code>isupper()<\/code>\u548c<code>islower()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5206\u522b\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5168\u90e8\u4e3a\u5927\u5199\u5b57\u6bcd\u6216\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p><code>isupper()<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u662f\u5426\u90fd\u662f\u5927\u5199\u5b57\u6bcd\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u5b57\u6bcd\u662f\u5c0f\u5199\u7684\uff0c\u6216\u8005\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u5b57\u6bcd\uff0c\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56deFalse\u3002\u76f8\u53cd\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u90fd\u662f\u5927\u5199\u7684\uff0c\u5219\u8fd4\u56deTrue\u3002\u7c7b\u4f3c\u5730\uff0c<code>islower()<\/code>\u65b9\u6cd5\u7528\u4e8e\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u6bcd\u662f\u5426\u90fd\u662f\u5c0f\u5199\u5b57\u6bcd\uff0c\u5176\u5de5\u4f5c\u539f\u7406\u4e0e<code>isupper()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5224\u5b9a\u5927\u5c0f\u5199\uff0c\u5e76\u4e14\u4f1a\u6db5\u76d6\u5176\u4ed6\u76f8\u5173\u7684\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u5224\u5b9a\u5927\u5c0f\u5199<\/h2>\n<\/p>\n<p><h3>1\u3001isupper()\u548cislower()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>isupper()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>isupper()<\/code>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u662f\u5426\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.isupper()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u662f\u5927\u5199\u5b57\u6bcd\uff0c\u5e76\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u5b57\u7b26\u662f\u5b57\u6bcd\uff0c\u90a3\u4e48\u8fd4\u56deTrue\uff1b\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HELLO&quot;<\/p>\n<p>print(text.isupper())  # \u8f93\u51fa: True<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>print(text.isupper())  # \u8f93\u51fa: False<\/p>\n<p>text = &quot;123&quot;<\/p>\n<p>print(text.isupper())  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>islower()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>islower()<\/code>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u662f\u5426\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.islower()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u662f\u5c0f\u5199\u5b57\u6bcd\uff0c\u5e76\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u5b57\u7b26\u662f\u5b57\u6bcd\uff0c\u90a3\u4e48\u8fd4\u56deTrue\uff1b\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello&quot;<\/p>\n<p>print(text.islower())  # \u8f93\u51fa: True<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>print(text.islower())  # \u8f93\u51fa: False<\/p>\n<p>text = &quot;123&quot;<\/p>\n<p>print(text.islower())  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001isalpha()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>isalpha()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u662f\u5426\u4e3a\u5b57\u6bcd\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.isalpha()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u662f\u5b57\u6bcd\uff08\u65e0\u8bba\u5927\u5c0f\u5199\uff09\uff0c\u8fd4\u56deTrue\uff1b\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HelloWorld&quot;<\/p>\n<p>print(text.isalpha())  # \u8f93\u51fa: True<\/p>\n<p>text = &quot;Hello123&quot;<\/p>\n<p>print(text.isalpha())  # \u8f93\u51fa: False<\/p>\n<p>text = &quot;123&quot;<\/p>\n<p>print(text.isalpha())  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001upper()\u548clower()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>upper()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>upper()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.upper()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello&quot;<\/p>\n<p>print(text.upper())  # \u8f93\u51fa: HELLO<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>print(text.upper())  # \u8f93\u51fa: HELLO<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>lower()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>lower()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u5927\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.lower()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;HELLO&quot;<\/p>\n<p>print(text.lower())  # \u8f93\u51fa: hello<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>print(text.lower())  # \u8f93\u51fa: hello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5224\u5b9a\u5927\u5c0f\u5199<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff0c\u7b80\u79f0regex\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7ec4\u5408\u7684\u6a21\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u641c\u7d22\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5339\u914d\u5927\u5199\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u8981\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u5927\u5199\u5b57\u6bcd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;[A-Z]&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;HelloWorld&quot;<\/p>\n<p>pattern = r&#39;[A-Z]&#39;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;H&#39;, &#39;W&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5339\u914d\u5c0f\u5199\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u8981\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;[a-z]&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;HelloWorld&quot;<\/p>\n<p>pattern = r&#39;[a-z]&#39;<\/p>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(matches)  # \u8f93\u51fa: [&#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;o&#39;, &#39;r&#39;, &#39;l&#39;, &#39;d&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5927\u5c0f\u5199\u5224\u5b9a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u6df7\u5408\u4e86\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def has_mixed_case(text):<\/p>\n<p>    has_upper = bool(re.search(r&#39;[A-Z]&#39;, text))<\/p>\n<p>    has_lower = bool(re.search(r&#39;[a-z]&#39;, text))<\/p>\n<p>    return has_upper and has_lower<\/p>\n<p>text = &quot;HelloWorld&quot;<\/p>\n<p>print(has_mixed_case(text))  # \u8f93\u51fa: True<\/p>\n<p>text = &quot;HELLO&quot;<\/p>\n<p>print(has_mixed_case(text))  # \u8f93\u51fa: False<\/p>\n<p>text = &quot;hello&quot;<\/p>\n<p>print(has_mixed_case(text))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><h3>1\u3001\u5bc6\u7801\u5f3a\u5ea6\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u5728\u7528\u6237\u6ce8\u518c\u6216\u4fee\u6539\u5bc6\u7801\u65f6\uff0c\u901a\u5e38\u9700\u8981\u68c0\u67e5\u5bc6\u7801\u7684\u5f3a\u5ea6\u3002\u4e00\u4e2a\u5f3a\u5bc6\u7801\u901a\u5e38\u8981\u6c42\u5305\u542b\u5927\u5199\u5b57\u6bcd\u3001\u5c0f\u5199\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u7279\u6b8a\u5b57\u7b26\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_strong_password(password):<\/p>\n<p>    if len(password) &lt; 8:<\/p>\n<p>        return False<\/p>\n<p>    has_upper = bool(re.search(r&#39;[A-Z]&#39;, password))<\/p>\n<p>    has_lower = bool(re.search(r&#39;[a-z]&#39;, password))<\/p>\n<p>    has_digit = bool(re.search(r&#39;\\d&#39;, password))<\/p>\n<p>    has_special = bool(re.search(r&#39;[!@#$%^&amp;*(),.?&quot;:{}|&lt;&gt;]&#39;, password))<\/p>\n<p>    return has_upper and has_lower and has_digit and has_special<\/p>\n<p>password = &quot;HelloWorld123!&quot;<\/p>\n<p>print(is_strong_password(password))  # \u8f93\u51fa: True<\/p>\n<p>password = &quot;helloworld&quot;<\/p>\n<p>print(is_strong_password(password))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6587\u672c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5904\u7406\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u9886\u57df\uff0c\u5224\u5b9a\u5927\u5c0f\u5199\u5b57\u7b26\u53ef\u4ee5\u7528\u4e8e\u6807\u8bb0\u5b9e\u4f53\u3001\u53e5\u5b50\u5206\u5272\u7b49\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u53e5\u5b50\u7684\u9996\u5b57\u6bcd\u901a\u5e38\u662f\u5927\u5199\u7684\uff0c\u901a\u8fc7\u5224\u5b9a\u5927\u5c0f\u5199\u53ef\u4ee5\u5e2e\u52a9\u5206\u5272\u6bb5\u843d\u4e2d\u7684\u53e5\u5b50\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_sentences(text):<\/p>\n<p>    sentences = []<\/p>\n<p>    start = 0<\/p>\n<p>    for i in range(1, len(text)):<\/p>\n<p>        if text[i].isupper() and text[i - 1] in &#39;.!?&#39;:<\/p>\n<p>            sentences.append(text[start:i].strip())<\/p>\n<p>            start = i<\/p>\n<p>    sentences.append(text[start:].strip())<\/p>\n<p>    return sentences<\/p>\n<p>text = &quot;Hello world! How are you? This is an example.&quot;<\/p>\n<p>print(split_sentences(text))<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;Hello world!&#39;, &#39;How are you?&#39;, &#39;This is an example.&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u5bf9\u6587\u672c\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5c06\u6240\u6709\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\uff0c\u4ee5\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def normalize_text(text):<\/p>\n<p>    return text.lower()<\/p>\n<p>text = &quot;Hello World&quot;<\/p>\n<p>print(normalize_text(text))  # \u8f93\u51fa: hello world<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6848<\/h2>\n<\/p>\n<p><h3>1\u3001\u5904\u7406\u975e\u5b57\u6bcd\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u5982\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u975e\u5b57\u6bcd\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_non_alpha(text):<\/p>\n<p>    return re.sub(r&#39;[^a-zA-Z]&#39;, &#39;&#39;, text)<\/p>\n<p>text = &quot;Hello, World! 123&quot;<\/p>\n<p>print(remove_non_alpha(text))  # \u8f93\u51fa: HelloWorld<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u65f6\uff0c\u9700\u8981\u8003\u8651\u4e0d\u540c\u8bed\u8a00\u7684\u5927\u5c0f\u5199\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u5fb7\u8bed\u4e2d\u7684\u201c\u00df\u201d\u6ca1\u6709\u5927\u5199\u5f62\u5f0f\uff0c\u571f\u8033\u5176\u8bed\u4e2d\u7684\u201c\u0131\u201d\u548c\u201cI\u201d\u662f\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>str.upper()<\/code>\u548c<code>str.lower()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u7279\u6b8a\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;stra\u00dfe&quot;<\/p>\n<p>print(text.upper())  # \u8f93\u51fa: STRASSE<\/p>\n<p>text = &quot;\u0130stanbul&quot;<\/p>\n<p>print(text.lower())  # \u8f93\u51fa: istanbul<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6587\u672c\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7b49\u9ad8\u6548\u7684Python\u8bed\u6cd5\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_upper_case(text):<\/p>\n<p>    return all(c.isupper() for c in text if c.isalpha())<\/p>\n<p>text = &quot;HELLO&quot;<\/p>\n<p>print(is_upper_case(text))  # \u8f93\u51fa: True<\/p>\n<p>text = &quot;Hello&quot;<\/p>\n<p>print(is_upper_case(text))  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u5b9a\u5927\u5c0f\u5199\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>isupper()<\/code>\u65b9\u6cd5\u3001<code>islower()<\/code>\u65b9\u6cd5\u3001<code>isalpha()<\/code>\u65b9\u6cd5\u3001<code>upper()<\/code>\u65b9\u6cd5\u548c<code>lower()<\/code>\u65b9\u6cd5<\/strong>\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u5bc6\u7801\u5f3a\u5ea6\u68c0\u67e5\u3001\u6587\u672c\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u5224\u5b9a\u5927\u5c0f\u5199\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u5e76\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u672f\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u4e2d\u5224\u5b9a\u5927\u5c0f\u5199\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\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u662f\u5168\u5927\u5199\u6216\u5168\u5c0f\u5199\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>isupper()<\/code>\u548c<code>islower()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5168\u90e8\u4e3a\u5927\u5199\u6216\u5c0f\u5199\u3002\u4f8b\u5982\uff0c<code>&quot;HELLO&quot;.isupper()<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\uff0c\u800c<code>&quot;hello&quot;.islower()<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u5b57\u6bcd\u5b57\u7b26\uff0c\u7ed3\u679c\u4f1a\u6839\u636e\u5b57\u6bcd\u90e8\u5206\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u540c\u65f6\u5305\u542b\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>any()<\/code>\u51fd\u6570\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u540c\u65f6\u5b58\u5728\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\u3002\u793a\u4f8b\u4ee3\u7801\u4e3a\uff1a<code>any(c.isupper() for c in s) and any(c.islower() for c in s)<\/code>\uff0c\u5176\u4e2d<code>s<\/code>\u4e3a\u5f85\u5224\u65ad\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u8fd9\u4e24\u4e2a\u6761\u4ef6\u90fd\u4e3a\u771f\uff0c\u8bf4\u660e\u5b57\u7b26\u4e32\u4e2d\u540c\u65f6\u5305\u542b\u5927\u5199\u548c\u5c0f\u5199\u5b57\u6bcd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8f6c\u6362\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5199\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f6c\u6362\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5199\u3002\u53ef\u4ee5\u4f7f\u7528<code>upper()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5168\u5927\u5199\uff0c\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5168\u5c0f\u5199\u3002\u82e5\u9700\u8981\u5c06\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff0c\u4f7f\u7528<code>title()<\/code>\u65b9\u6cd5\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>s.upper()<\/code>\u5c06\u8fd4\u56de\u5b57\u7b26\u4e32<code>s<\/code>\u7684\u5168\u5927\u5199\u5f62\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u5b9a\u5927\u5c0f\u5199\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684isupp [&hellip;]","protected":false},"author":3,"featured_media":1179582,"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\/1179573"}],"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=1179573"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179573\/revisions"}],"predecessor-version":[{"id":1179583,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1179573\/revisions\/1179583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1179582"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1179573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1179573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1179573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}