{"id":2514,"date":"2025-06-01T17:32:20","date_gmt":"2025-06-04T10:05:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2514"},"modified":"2025-06-04T17:32:20","modified_gmt":"2025-06-04T10:05:19","slug":"python-string-startswith-endswith","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-string-startswith-endswith\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6709\u6548\u4f7f\u7528 startswith() \u548c endswith() \u51fd\u6578\u5224\u65b7\u5b57\u4e32\u958b\u982d\u8207\u7d50\u5c3e"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>\u4f7f\u7528 Python String \u5b57\u4e32\u7684 startswith() \u548c endswith() \u8a9e\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>startswith()<\/strong> \u548c <strong>endswith()<\/strong> \u51fd\u6578\u4f86\u5224\u65b7\u5b57\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u7684\u5b57\u5143\u958b\u982d\u6216\u7d50\u5c3e\u3002\u9019\u5169\u500b\u51fd\u6578\u975e\u5e38\u7c21\u55ae\u6613\u7528\uff0c\u9069\u5408\u7528\u65bc\u8655\u7406\u5b57\u4e32\u7684\u57fa\u672c\u9700\u6c42\u3002<\/p>\n<h2>\u4f7f\u7528 startswith() \u51fd\u6578\u5224\u65b7\u5b57\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u5b57\u5143\u958b\u982d<\/h2>\n<p>\u4f7f\u7528 <strong>startswith()<\/strong> \u51fd\u6578\u6642\uff0c\u6211\u5011\u9700\u8981\u50b3\u5165\u4e00\u500b\u5b57\u5143\u6216\u5b57\u4e32\u4f5c\u70ba\u5224\u65b7\u7684\u6a19\u6e96\u3002\u5982\u679c\u5b57\u4e32\u4ee5\u50b3\u5165\u7684\u5b57\u5143\u6216\u5b57\u4e32\u958b\u982d\uff0c\u5247\u6703\u50b3\u56de <code>True<\/code>\uff1b\u5426\u5247\u8fd4\u56de <code>False<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Hello, World!\"\nresult = text.startswith(\"H\")\nprint(result)  # \u986f\u793a\uff1aTrue\n\nresult = text.startswith(\"h\")\nprint(result)  # \u986f\u793a\uff1aFalse\n\nresult = text.startswith(\"Hello\")\nprint(result)  # \u986f\u793a\uff1aTrue\n<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> <strong>startswith()<\/strong> \u51fd\u6578\u662f\u5927\u5c0f\u5beb\u654f\u611f\u7684\uff0c\u56e0\u6b64\u5728\u9032\u884c\u6bd4\u8f03\u6642\u5fc5\u9808\u6ce8\u610f\u5927\u5c0f\u5beb\u3002<\/p>\n<h2>\u4f7f\u7528 endswith() \u51fd\u6578\u5224\u65b7\u5b57\u4e32\u662f\u5426\u4ee5\u7279\u5b9a\u5b57\u5143\u7d50\u5c3e<\/h2>\n<p>\u4f7f\u7528 <strong>endswith()<\/strong> \u51fd\u6578\u6642\uff0c\u4ea6\u9700\u50b3\u5165\u4e00\u500b\u5b57\u5143\u6216\u5b57\u4e32\u4f5c\u70ba\u5224\u65b7\u7684\u6a19\u6e96\u3002\u5982\u679c\u5b57\u4e32 <code>text<\/code> \u4ee5\u50b3\u5165\u7684\u5b57\u5143\u6216\u5b57\u4e32\u7d50\u5c3e\uff0c\u5247 <strong>endswith()<\/strong> \u51fd\u6578\u6703\u50b3\u56de <code>True<\/code>\uff0c\u5426\u5247\u8fd4\u56de <code>False<\/code>\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u78bc\u7528\u4f86\u5224\u65b7\u5b57\u4e32\u7684\u7d50\u5c3e\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Hello, World!\"\nresult = text.endswith('a')\nprint(result)  # \u986f\u793a\uff1aFalse\n\nresult = text.endswith('orld!')\nprint(result)  # \u986f\u793a\uff1aTrue\n<\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528 <strong>endswith()<\/strong> \u51fd\u6578\u4f86\u5224\u65b7\u5b57\u4e32\u662f\u5426\u4ee5\u591a\u500b\u5b57\u4e32\u7d50\u5c3e\uff0c\u900f\u904e\u50b3\u5165\u4e00\u500b tuple \u4f86\u5be6\u73fe\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Hello, World!\"\nresult = text.endswith((\".txt\", \".pdf\"))  # False\nresult = text.endswith((\"!\", \"?\"))  # True\n<\/pre>\n<h2>\u8655\u7406\u932f\u8aa4\u8207\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u9019\u5169\u500b\u51fd\u6578\u6642\uff0c\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u4e0d\u662f\u5b57\u4e32\u985e\u578b\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Hello, World!\"\n\n# \u50b3\u5165\u6578\u5b57\nresult = text.endswith(123)  # TypeError: endswith first arg must be str or a tuple of str, not int\n\n# \u50b3\u5165\u5e03\u6797\u503c\nresult = text.startswith(True)  # TypeError: startswith first arg must be str or a tuple of str, not bool\n<\/pre>\n<p>\u56e0\u6b64\uff0c\u78ba\u4fdd\u50b3\u5165\u7684\u53c3\u6578\u70ba\u5b57\u4e32\u985e\u578b\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u9032\u968e\u61c9\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u969b\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6709\u6548\u4f7f\u7528 <strong>startswith()<\/strong> \u548c <strong>endswith()<\/strong> \u51fd\u6578\uff1a<\/p>\n<pre class=\"brush: python\">\n# \u5224\u65b7\u6587\u4ef6\u540d\u662f\u5426\u4ee5 \".txt\" \u70ba\u7d50\u5c3e\nfilename = \"example.txt\"\nresult = filename.endswith(\".txt\")  # result \u70ba True\n\n# \u5224\u65b7\u6587\u4ef6\u540d\u662f\u5426\u4ee5 \"example\" \u958b\u982d\nresult = filename.startswith(\"example\")  # result \u70ba True\n\n# \u5224\u65b7\u6587\u4ef6\u540d\u662f\u5426\u4ee5 \"EXAMPLE\" \u958b\u982d\nresult = filename.startswith(\"EXAMPLE\")  # result \u70ba False\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u7528 <strong>endswith()<\/strong> \u51fd\u6578\u4f86\u6aa2\u67e5\u6587\u4ef6\u540d\u7684\u7d50\u5c3e\uff0c\u4e26\u78ba\u8a8d\u5b83\u662f\u5426\u662f\u4ee5 &#8220;.txt&#8221; \u70ba\u7d50\u5c3e\u3002<\/p>\n<h2>\u5c0f\u7d50<\/h2>\n<p><strong>startswith()<\/strong> \u548c <strong>endswith()<\/strong> \u51fd\u6578\u7684\u53c3\u6578\u662f\u53ef\u9078\u7684\uff0c\u5982\u679c\u4e0d\u50b3\u5165\u53c3\u6578\uff0c\u9810\u8a2d\u6703\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u4ee5\u7a7a\u767d\u5b57\u5143\u7d50\u5c3e\u6216\u958b\u982d\u3002\u4ee5\u4e0b\u662f\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"   Hello, World!   \"\nresult = text.startswith()  # \u8fd4\u56de True\uff0c\u56e0\u70ba\u5b57\u4e32\u4ee5\u7a7a\u767d\u5b57\u5143\u958b\u982d\nresult = text.endswith()  # \u8fd4\u56de True\uff0c\u56e0\u70ba\u5b57\u4e32\u4ee5\u7a7a\u767d\u5b57\u5143\u7d50\u5c3e\n<\/pre>\n<p>\u9019\u4e9b\u7c21\u55ae\u800c\u9748\u6d3b\u7684\u529f\u80fd\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5728\u7a0b\u5f0f\u958b\u767c\u4e2d\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u5b57\u4e32\u3002\u82e5\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u5b57\u4e32\u65b9\u6cd5\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc\/article\/5f5d2728fd89780001e1c4eb) \u7684\u76f8\u95dc\u6559\u5b78\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. startswith() \u548c endswith() \u53ef\u4ee5\u63a5\u53d7\u54ea\u4e9b\u985e\u578b\u7684\u53c3\u6578\uff1f<\/h3>\n<p>\u9019\u5169\u500b\u51fd\u6578\u4e3b\u8981\u63a5\u53d7\u5b57\u4e32\u985e\u578b\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u4e5f\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u7684 tuple \u4f86\u9032\u884c\u591a\u500b\u5b57\u4e32\u7684\u5224\u65b7\u3002<\/p>\n<h3>2. \u5982\u679c\u6211\u50b3\u5165\u7684\u53c3\u6578\u4e0d\u662f\u5b57\u4e32\u6703\u600e\u6a23\uff1f<\/h3>\n<p>\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u4e0d\u662f\u5b57\u4e32\uff0c\u5c07\u6703\u5f15\u767c TypeError \u932f\u8aa4\uff0c\u63d0\u793a\u4f60\u5fc5\u9808\u50b3\u5165\u5b57\u4e32\u985e\u578b\u7684\u53c3\u6578\u3002<\/p>\n<h3>3. \u6211\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u4f7f\u7528 startswith() \u548c endswith() \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5c0e\u5f0f\u4f86\u904e\u6ffe\u7b26\u5408\u689d\u4ef6\u7684\u5b57\u4e32\uff0c\u4f8b\u5982\u7be9\u9078\u51fa\u6240\u6709\u4ee5\u7279\u5b9a\u5b57\u4e32\u958b\u982d\u6216\u7d50\u5c3e\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`html \u4f7f\u7528 Python St&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[],"class_list":["post-2514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ey","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2514"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2514\/revisions"}],"predecessor-version":[{"id":13065,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2514\/revisions\/13065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}