{"id":2561,"date":"2025-05-31T17:41:17","date_gmt":"2025-06-04T10:03:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2561"},"modified":"2025-06-04T17:41:17","modified_gmt":"2025-06-04T10:03:16","slug":"python-string-index-rindex","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-string-index-rindex\/","title":{"rendered":"\u3010\u6df1\u5165\u63a2\u8a0e Python \u5b57\u4e32\u7684 index() \u548c rindex() \u51fd\u6578\uff1a\u67e5\u627e\u5b50\u5b57\u4e32\u4f4d\u7f6e\u7684\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>\u6df1\u5165\u63a2\u8a0e Python \u5b57\u4e32\u7684 index() \u548c rindex() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c<strong>index()<\/strong> \u548c <strong>rindex()<\/strong> \u51fd\u6578\u662f\u7528\u4f86\u67e5\u627e\u5b57\u4e32\u4e2d\u5b50\u5b57\u4e32\u4f4d\u7f6e\u7684\u91cd\u8981\u5de5\u5177\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6703\u5e36\u4f60\u4e86\u89e3\u9019\u5169\u500b\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u907f\u514d\u5e38\u898b\u7684\u932f\u8aa4\u3002<\/p>\n<h2>\u4f7f\u7528 index() \u51fd\u6578<\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u5b57\u4e32\u4e2d\u67e5\u627e\u7279\u5b9a\u7684\u5b50\u5b57\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>text.index(\"\u5b50\u5b57\u4e32\")<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5728\u5b57\u4e32\u4e2d\u67e5\u627e &#8220;is&#8221; \u9019\u500b\u5b50\u5b57\u4e32\uff0c\u53ef\u4ee5\u9019\u6a23\u505a\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Python is awesome\"\npos = text.index(\"is\")\nprint(pos)  # \u986f\u793a\uff1a7\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c<code>text.index(\"is\")<\/code> \u6703\u8fd4\u56de &#8220;is&#8221; \u5728\u5b57\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5373\u7b2c 7 \u500b\u5b57\u7b26\u7684\u7d22\u5f15\u3002<\/p>\n<h2>\u4f7f\u7528 rindex() \u51fd\u6578<\/h2>\n<p>\u8207 <code>index()<\/code> \u76f8\u5c0d\uff0c<strong>rindex()<\/strong> \u51fd\u6578\u5f9e\u5b57\u4e32\u7684\u672b\u5c3e\u958b\u59cb\u67e5\u627e\u5b50\u5b57\u4e32\u7684\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 <code>rindex()<\/code> \u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Python is awesome\"\npos = text.rindex(\"is\")\nprint(pos)  # \u986f\u793a\uff1a7\n<\/pre>\n<p>\u9019\u88e1\uff0c<code>text.rindex(\"is\")<\/code> \u540c\u6a23\u8fd4\u56de &#8220;is&#8221; \u7684\u4f4d\u7f6e\uff0c\u4f46\u67e5\u627e\u904e\u7a0b\u662f\u5f9e\u5b57\u4e32\u7684\u672b\u5c3e\u958b\u59cb\u7684\u3002<\/p>\n<h2>\u6307\u5b9a\u67e5\u627e\u7684\u8d77\u59cb\u4f4d\u7f6e<\/h2>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u67e5\u627e\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\ntext = \"Python is awesome\"\npos = text.index(\"is\", 8)\nprint(pos)  # \u986f\u793a\uff1a-1\n<\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c<code>text.index(\"is\", 8)<\/code> \u5f9e\u7d22\u5f15 8 \u958b\u59cb\u67e5\u627e &#8220;is&#8221;\uff0c\u56e0\u70ba\u5728\u9019\u500b\u4f4d\u7f6e\u4e4b\u5f8c\u4e26\u4e0d\u5b58\u5728 &#8220;is&#8221;\uff0c\u6240\u4ee5\u8fd4\u56de -1\u3002<\/p>\n<h2>\u7570\u5e38\u8655\u7406<\/h2>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u67e5\u627e\u7684\u5b50\u5b57\u4e32\u4e0d\u5b58\u5728\uff0c<strong>index()<\/strong> \u548c <strong>rindex()<\/strong> \u6703\u62cb\u51fa <code>ValueError<\/code>\u3002\u70ba\u4e86\u9632\u6b62\u7a0b\u5f0f\u5d29\u6f70\uff0c\u5efa\u8b70\u4f7f\u7528 <code>try...except<\/code> \u8a9e\u53e5\u4f86\u6355\u6349\u9019\u4e9b\u7570\u5e38\uff1a<\/p>\n<pre class=\"brush: python\">\ntry:\n    pos = text.index(\"notfound\")\nexcept ValueError:\n    print(\"\u5b50\u5b57\u4e32\u672a\u627e\u5230\uff01\")\n<\/pre>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9019\u5169\u500b\u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u5e6b\u52a9\u4f60\u67e5\u627e\u5b50\u5b57\u4e32\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u8cc7\u6599\u6e05\u7406\u548c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u8655\u7406\u7528\u6236\u8f38\u5165\u6642\u78ba\u4fdd\u7279\u5b9a\u5b57\u4e32\u7684\u5b58\u5728\u6027\uff0c\u6216\u5728\u5206\u6790\u6587\u672c\u6578\u64da\u6642\u7372\u53d6\u95dc\u9375\u5b57\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u7a0b\u5f0f\u8a2d\u8a08\u6559\u5b78\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/vocus.cc\/article\/605c2f7cf6d0f40eae7d1bfc\" target=\"_blank\" rel=\"noopener\">Python \u5b57\u4e32\u8655\u7406\u9032\u968e<\/a>\u3002<\/p>\n<h2>\u5c0f\u7d50<\/h2>\n<p>\u7e3d\u7d50\u4e00\u4e0b\uff0c<strong>index()<\/strong> \u51fd\u6578\u53ef\u4ee5\u5728\u5b57\u4e32\u4e2d\u67e5\u627e\u5b50\u5b57\u4e32\u7684\u4f4d\u7f6e\uff0c\u800c <strong>rindex()<\/strong> \u51fd\u6578\u5247\u662f\u5f9e\u5b57\u4e32\u7684\u672b\u5c3e\u958b\u59cb\u67e5\u627e\u3002\u9019\u5169\u500b\u51fd\u6578\u5c0d\u65bc\u5b57\u4e32\u64cd\u4f5c\u975e\u5e38\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6587\u672c\u6578\u64da\u6642\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u679c\u6211\u4f7f\u7528 index() \u4f46\u5b50\u5b57\u4e32\u4e0d\u5b58\u5728\uff0c\u6703\u767c\u751f\u4ec0\u9ebc\u60c5\u6cc1\uff1f<\/h3>\n<p>\u6703\u62cb\u51fa <code>ValueError<\/code>\uff0c\u56e0\u6b64\u5efa\u8b70\u4f7f\u7528 <code>try...except<\/code> \u4f86\u8655\u7406\u9019\u7a2e\u60c5\u6cc1\u3002<\/p>\n<h3>2. index() \u548c rindex() \u6709\u4ec0\u9ebc\u4e3b\u8981\u5dee\u7570\uff1f<\/h3>\n<p>\u4e3b\u8981\u5dee\u7570\u5728\u65bc\u67e5\u627e\u7684\u65b9\u5411\uff1a<code>index()<\/code> \u5f9e\u958b\u982d\u958b\u59cb\u67e5\u627e\uff0c\u800c <code>rindex()<\/code> \u5f9e\u672b\u5c3e\u958b\u59cb\u67e5\u627e\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u67e5\u627e\u6642\u6307\u5b9a\u8d77\u59cb\u4f4d\u7f6e\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728\u51fd\u6578\u4e2d\u50b3\u5165\u7b2c\u4e8c\u500b\u53c3\u6578\u4f86\u6307\u5b9a\u67e5\u627e\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u4f8b\u5982 <code>text.index(\"\u5b50\u5b57\u4e32\", \u8d77\u59cb\u4f4d\u7f6e)<\/code>\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`html \u6df1\u5165\u63a2\u8a0e Python &#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-2561","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-Fj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2561","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=2561"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2561\/revisions"}],"predecessor-version":[{"id":13059,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2561\/revisions\/13059"}],"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=2561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}