{"id":3104,"date":"2025-06-01T16:46:51","date_gmt":"2025-06-04T08:54:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3104"},"modified":"2025-06-04T16:46:51","modified_gmt":"2025-06-04T08:54:51","slug":"%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84index%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84index%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2 Python \u7684 `index()` \u51fd\u5f0f\uff1a\u7528\u6cd5\u3001\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, index(), \u5b57\u4e32\u641c\u5c0b, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u5b78\u7fd2 Python \u7684 `index()` \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`index()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u7528\u65bc\u641c\u5c0b\u5b57\u4e32\u4e2d\u6307\u5b9a\u5b57\u5143\u7684\u4f4d\u7f6e\u3002\u9019\u500b\u51fd\u5f0f\u6703\u56de\u50b3\u7b2c\u4e00\u500b\u7b26\u5408\u689d\u4ef6\u7684\u5b57\u5143\u7684\u7d22\u5f15\u503c\uff0c\u5982\u679c\u6c92\u6709\u627e\u5230\uff0c\u5247\u6703\u5f15\u767c `ValueError`\u3002<\/p>\n<p>## \u8a9e\u6cd5\u8207\u53c3\u6578<br \/>\n`index()` \u51fd\u5f0f\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\nstr.index(sub[, start[, end]])\n<\/pre>\n<p>&#8211; **sub**: \u5fc5\u586b\uff0c\u6307\u5b9a\u8981\u641c\u5c0b\u7684\u5b50\u5b57\u4e32\u3002<br \/>\n&#8211; **start**: \u53ef\u9078\uff0c\u6307\u5b9a\u641c\u5c0b\u7684\u8d77\u59cb\u7d22\u5f15\uff08\u9ed8\u8a8d\u70ba\u5b57\u4e32\u7684\u958b\u59cb\u4f4d\u7f6e\uff09\u3002<br \/>\n&#8211; **end**: \u53ef\u9078\uff0c\u6307\u5b9a\u641c\u5c0b\u7684\u7d50\u675f\u7d22\u5f15\uff08\u9ed8\u8a8d\u70ba\u5b57\u4e32\u7684\u7d50\u675f\u4f4d\u7f6e\uff09\u3002<\/p>\n<p>## \u57fa\u672c\u7bc4\u4f8b<br \/>\n\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `index()` \u51fd\u5f0f\uff1a<\/p>\n<pre class=\"brush: python\">\nstr = \"Hello World\"\n\n# \u641c\u5c0b\u5b57\u5143 'o'\nprint(str.index('o'))  # \u8f38\u51fa: 4\n\n# \u641c\u5c0b\u5b57\u5143 'W'\nprint(str.index('W'))  # \u8f38\u51fa: 6\n\n# \u5617\u8a66\u641c\u5c0b\u5b57\u5143 'z'\ntry:\n    print(str.index('z'))\nexcept ValueError as e:\n    print(e)  # \u8f38\u51fa: substring not found\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5b57\u4e32\u4e2d\u7684 &#8216;o&#8217; \u548c &#8216;W&#8217; \u90fd\u88ab\u6b63\u78ba\u627e\u5230\uff0c\u800c &#8216;z&#8217; \u5247\u56e0\u70ba\u4e0d\u5b58\u5728\u65bc\u5b57\u4e32\u4e2d\u800c\u5f15\u767c `ValueError`\u3002<\/p>\n<p>## \u6307\u5b9a\u641c\u5c0b\u7bc4\u570d<br \/>\n`index()` \u51fd\u5f0f\u5141\u8a31\u6211\u5011\u6307\u5b9a\u641c\u5c0b\u7bc4\u570d\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nstr = \"Hello World\"\n\n# \u5f9e\u7d22\u5f15\u503c 5 \u958b\u59cb\u641c\u5c0b\u5b57\u5143 'o'\nprint(str.index('o', 5))  # \u8f38\u51fa: 7\n\n# \u5f9e\u7d22\u5f15\u503c 0 \u5230\u7d22\u5f15\u503c 5 \u641c\u5c0b\u5b57\u5143 'o'\ntry:\n    print(str.index('o', 0, 5))\nexcept ValueError as e:\n    print(e)  # \u8f38\u51fa: substring not found\n<\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u7b2c\u4e00\u500b\u7bc4\u4f8b\u4e2d\u5f9e\u7d22\u5f15\u503c 5 \u958b\u59cb\u641c\u5c0b &#8216;o&#8217;\uff0c\u7d50\u679c\u70ba 7\uff1b\u800c\u7b2c\u4e8c\u500b\u7bc4\u4f8b\u5247\u56e0\u70ba\u5728\u6307\u5b9a\u7bc4\u570d\u5167\u672a\u627e\u5230 &#8216;o&#8217;\uff0c\u56e0\u6b64\u5831\u932f\u3002<\/p>\n<p>## \u932f\u8aa4\u8655\u7406\u8207\u6700\u4f73\u5be6\u8e10<br \/>\n\u4f7f\u7528 `index()` \u51fd\u5f0f\u6642\uff0c\u5efa\u8b70\u642d\u914d `try&#8230;except` \u7d50\u69cb\u4f86\u8655\u7406\u53ef\u80fd\u7684 `ValueError`\u3002\u9019\u6a23\u53ef\u4ee5\u4f7f\u7a0b\u5f0f\u66f4\u52a0\u5065\u58ef\uff0c\u907f\u514d\u56e0\u70ba\u672a\u627e\u5230\u5b57\u5143\u800c\u5c0e\u81f4\u7a0b\u5f0f\u5d29\u6f70\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<br \/>\n`index()` \u51fd\u5f0f\u53ef\u4ee5\u8207\u5176\u4ed6\u5b57\u4e32\u8655\u7406\u51fd\u5f0f\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; **`count()`**: \u8a08\u7b97\u5b57\u5143\u51fa\u73fe\u7684\u6b21\u6578\u3002<br \/>\n&#8211; **`find()`**: \u76f8\u4f3c\uff0c\u4f46\u5728\u672a\u627e\u5230\u6642\u8fd4\u56de -1 \u800c\u975e\u5f15\u767c\u932f\u8aa4\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u53c3\u8003\u66f4\u8a73\u7d30\u7684 Python \u5b57\u4e32\u8655\u7406\u6559\u5b78\uff0c\u4e86\u89e3\u66f4\u591a\u76f8\u95dc\u5167\u5bb9\uff1a[Python \u5b57\u4e32\u8655\u7406\u6559\u5b78](https:\/\/vocus.cc\/article\/5f8d4c1d0d5a26001b9c9e28)\u3002<\/p>\n<p>## \u7d50\u8ad6<br \/>\nPython \u7684 `index()` \u51fd\u5f0f\u662f\u4e00\u500b\u5f37\u5927\u7684\u5b57\u4e32\u641c\u5c0b\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u5feb\u901f\u627e\u5230\u5b57\u5143\u4f4d\u7f6e\u3002\u901a\u904e\u6b63\u78ba\u7684\u4f7f\u7528\u548c\u932f\u8aa4\u8655\u7406\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u7a0b\u5f0f\u4e2d\u9ad8\u6548\u5730\u8655\u7406\u5b57\u4e32\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `index()` \u8207 `find()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `index()` \u5728\u672a\u627e\u5230\u6642\u6703\u5f15\u767c `ValueError`\uff0c\u800c `find()` \u5247\u6703\u8fd4\u56de -1\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u641c\u5c0b\u6642\u5ffd\u7565\u5927\u5c0f\u5beb\uff1f**<br \/>\nA2: \u53ef\u4ee5\u5c07\u5b57\u4e32\u548c\u641c\u5c0b\u7684\u5b50\u5b57\u4e32\u90fd\u8f49\u63db\u70ba\u5c0f\u5beb\u6216\u5927\u5beb\uff0c\u4f8b\u5982\u4f7f\u7528 `str.lower()`\u3002<\/p>\n<p>**Q3: \u662f\u5426\u53ef\u4ee5\u641c\u5c0b\u591a\u500b\u5b57\u5143\uff1f**<br \/>\nA3: `index()` \u53ea\u80fd\u641c\u5c0b\u55ae\u4e00\u5b50\u5b57\u4e32\u3002\u5982\u679c\u9700\u8981\u641c\u5c0b\u591a\u500b\u5b57\u5143\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u8ff4\u5708\u6216\u5176\u4ed6\u5b57\u4e32\u8655\u7406\u65b9\u6cd5\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Index()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5728\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7d44\u7b49\u5e8f\u5217\u4e2d\u641c\u7d22\u6307\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u4e26\u8fd4\u56de\u5176\u7d22\u5f15\u503c\u3002\u672c\u6587\u5c07\u4ecb\u7d39Index()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u641c\u7d22\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","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":[15],"class_list":["post-3104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-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-O4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3104","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=3104"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3104\/revisions"}],"predecessor-version":[{"id":12931,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3104\/revisions\/12931"}],"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=3104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}