{"id":1143514,"date":"2025-01-08T22:51:04","date_gmt":"2025-01-08T14:51:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143514.html"},"modified":"2025-01-08T22:51:06","modified_gmt":"2025-01-08T14:51:06","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e4%b8%80%e8%a1%8c%e6%9c%89%e5%a4%9a%e5%b0%91%e4%b8%aa%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143514.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u4e00\u884c\u6709\u591a\u5c11\u4e2a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181115\/855912e2-36a3-4420-84e3-02ca3c653572.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u4e00\u884c\u6709\u591a\u5c11\u4e2a\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7edf\u8ba1\u4e00\u884c\u6709\u591a\u5c11\u4e2a\u5b57\u7b26\u3001\u5e38\u7528\u7684\u6709len()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570len()\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u4e00\u70b9\uff0c\u5e76\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528len()\u51fd\u6570\u7edf\u8ba1\u5b57\u7b26\u6570\u91cf<\/p>\n<\/p>\n<p><p>Python\u7684len()\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5feb\u901f\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line = &quot;Hello, World!&quot;<\/p>\n<p>print(len(line))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0clen(line)\u5c06\u8fd4\u56de13\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\u5305\u542b13\u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u548c\u6807\u70b9\u7b26\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0len()\u51fd\u6570\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>len()\u51fd\u6570\u5728\u7edf\u8ba1\u5b57\u7b26\u6570\u91cf\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7edf\u8ba1\u666e\u901a\u5b57\u7b26\uff0c\u8fd8\u53ef\u4ee5\u7edf\u8ba1\u7a7a\u683c\u3001\u6807\u70b9\u7b26\u53f7\u4ee5\u53ca\u5404\u79cd\u7279\u6b8a\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528len()\u51fd\u6570\u7684\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line1 = &quot;Python is fun!&quot;<\/p>\n<p>line2 = &quot;1234567890&quot;<\/p>\n<p>line3 = &quot;     &quot;  # 5\u7a7a\u683c<\/p>\n<p>print(len(line1))  # \u8f93\u51fa 14<\/p>\n<p>print(len(line2))  # \u8f93\u51fa 10<\/p>\n<p>print(len(line3))  # \u8f93\u51fa 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u4f8b\u5b50\u4e2d\uff0clen()\u51fd\u6570\u51c6\u786e\u5730\u8fd4\u56de\u4e86\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u65e0\u8bba\u5b57\u7b26\u4e32\u5305\u542b\u4ec0\u4e48\u7c7b\u578b\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u7edf\u8ba1\u5b57\u7b26\u6570\u91cf<\/p>\n<\/p>\n<p><p>\u9664\u4e86len()\u51fd\u6570\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7edf\u8ba1\u7279\u5b9a\u7c7b\u578b\u7684\u5b57\u7b26\u6570\u91cf\u3002\u4f8b\u5982\uff0ccount()\u65b9\u6cd5\u53ef\u4ee5\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u867d\u7136count()\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u7edf\u8ba1\u6574\u4e2a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f46\u5b83\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line = &quot;banana&quot;<\/p>\n<p>print(line.count(&#39;a&#39;))  # \u8f93\u51fa 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cline.count(&#39;a&#39;)\u8fd4\u56de3\uff0c\u56e0\u4e3a\u5b57\u7b26&#39;a&#39;\u5728\u5b57\u7b26\u4e32&quot;banana&quot;\u4e2d\u51fa\u73b0\u4e86\u4e09\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u7ec4\u5408\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u548clen()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u7c7b\u578b\u5b57\u7b26\u7684\u6570\u91cf\uff0c\u7136\u540e\u518d\u4f7f\u7528len()\u51fd\u6570\u5f97\u5230\u5b57\u7b26\u4e32\u7684\u603b\u957f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line = &quot;Hello, World!&quot;<\/p>\n<p>letter_count = len(line) - line.count(&#39; &#39;)<\/p>\n<p>print(&quot;Total characters excluding spaces:&quot;, letter_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528len()\u51fd\u6570\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u603b\u957f\u5ea6\uff0c\u7136\u540e\u51cf\u53bb\u5b57\u7b26\u4e32\u4e2d\u7a7a\u683c\u7684\u6570\u91cf\uff0c\u5f97\u5230\u4e0d\u5305\u62ec\u7a7a\u683c\u7684\u5b57\u7b26\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7edf\u8ba1\u5b57\u7b26\u6570\u91cf<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\u3002Python\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u7edf\u8ba1\u7279\u5b9a\u7c7b\u578b\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u6216\u8fc7\u6ee4\u6389\u7279\u5b9a\u7c7b\u578b\u7684\u5b57\u7b26\u7136\u540e\u4f7f\u7528len()\u51fd\u6570\u7edf\u8ba1\u603b\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>line = &quot;Hello, World!&quot;<\/p>\n<p>non_space_chars = re.findall(r&#39;\\S&#39;, line)<\/p>\n<p>print(&quot;Total non-space characters:&quot;, len(non_space_chars))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cre.findall(r&#39;\\S&#39;, line)\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u8868\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u975e\u7a7a\u5b57\u7b26\uff0c\u7136\u540e\u4f7f\u7528len()\u51fd\u6570\u7edf\u8ba1\u8fd9\u4e9b\u975e\u7a7a\u5b57\u7b26\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u5339\u914d\u548c\u7edf\u8ba1\u4efb\u52a1\u65f6\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">line = &quot;Python 3.8.5&quot;<\/p>\n<h2><strong>\u7edf\u8ba1\u6570\u5b57\u5b57\u7b26\u7684\u6570\u91cf<\/strong><\/h2>\n<p>digits = re.findall(r&#39;\\d&#39;, line)<\/p>\n<p>print(&quot;Total digits:&quot;, len(digits))  # \u8f93\u51fa 4<\/p>\n<h2><strong>\u7edf\u8ba1\u5b57\u6bcd\u5b57\u7b26\u7684\u6570\u91cf<\/strong><\/h2>\n<p>letters = re.findall(r&#39;[a-zA-Z]&#39;, line)<\/p>\n<p>print(&quot;Total letters:&quot;, len(letters))  # \u8f93\u51fa 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e0d\u540c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\u548c\u5b57\u6bcd\u5b57\u7b26\uff0c\u7136\u540e\u4f7f\u7528len()\u51fd\u6570\u7edf\u8ba1\u8fd9\u4e9b\u5339\u914d\u5230\u7684\u5b57\u7b26\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<strong>\u5982\u679c\u53ea\u662f\u9700\u8981\u5feb\u901f\u7edf\u8ba1\u5b57\u7b26\u4e32\u957f\u5ea6\uff0clen()\u51fd\u6570\u662f\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u7edf\u8ba1\u7279\u5b9a\u7c7b\u578b\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong>\u65e0\u8bba\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u51c6\u786e\u5730\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6570\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6570\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6570\u91cf\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>len(&quot;\u60a8\u7684\u5b57\u7b26\u4e32&quot;)<\/code>\uff0c\u8fd9\u6837\u5c31\u80fd\u5feb\u901f\u83b7\u5f97\u8be5\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u603b\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u5ffd\u7565\u7a7a\u683c\u7edf\u8ba1\u5b57\u7b26\u6570\u91cf\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u7edf\u8ba1\u5b57\u7b26\u65f6\u4e0d\u5305\u62ec\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u5c06\u7a7a\u683c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<code>len(&quot;\u60a8\u7684\u5b57\u7b26\u4e32&quot;.replace(&quot; &quot;, &quot;&quot;))<\/code>\uff0c\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u4e0d\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u6570\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u7edf\u8ba1\u4e00\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>count()<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba1\u7279\u5b9a\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u7edf\u8ba1\u5b57\u7b26&#39;a&#39;\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<code>&quot;\u60a8\u7684\u5b57\u7b26\u4e32&quot;.count(&#39;a&#39;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u8be5\u5b57\u7b26\u5728\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7edf\u8ba1\u4e00\u884c\u6709\u591a\u5c11\u4e2a\u5b57\u7b26\u3001\u5e38\u7528\u7684\u6709len()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6700\u7b80 [&hellip;]","protected":false},"author":3,"featured_media":1143519,"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\/1143514"}],"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=1143514"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143514\/revisions"}],"predecessor-version":[{"id":1143520,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143514\/revisions\/1143520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}