[{"data":1,"prerenderedAt":1672},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-string-index-method":3},{"id":4,"title":5,"body":6,"description":1665,"extension":1666,"meta":1667,"navigation":122,"path":1668,"seo":1669,"stem":1670,"__hash__":1671},"content\u002Freference\u002Fpython-string-index-method.md","Python String index() Method",{"type":7,"value":8,"toc":1636},"minimark",[9,19,26,33,38,137,145,153,158,161,180,183,259,262,281,284,337,341,344,376,379,398,401,444,447,495,504,554,558,568,571,590,592,689,698,739,745,755,763,771,774,779,884,887,911,915,970,973,976,983,991,993,1035,1038,1059,1073,1082,1170,1173,1183,1192,1199,1204,1212,1259,1264,1274,1340,1345,1350,1363,1367,1372,1402,1405,1509,1512,1533,1537,1544,1547,1551,1556,1565,1576,1583,1586,1593,1596,1600,1632],[10,11,13,14,18],"h1",{"id":12},"python-string-index-method","Python String ",[15,16,17],"code",{},"index()"," Method",[20,21,22,23,25],"p",{},"The Python string ",[15,24,17],{}," method returns the position of the first match inside a string.",[20,27,28,29,32],{},"Use it when you want to find where a character or substring appears, and you expect that value to be present. If Python cannot find the value, it raises a ",[15,30,31],{},"ValueError",".",[34,35,37],"h2",{"id":36},"quick-example","Quick example",[39,40,45],"pre",{"className":41,"code":42,"language":43,"meta":44,"style":44},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","text = \"hello world\"\nposition = text.index(\"world\")\nprint(position)\n\n# Output:\n# 6\n","python","",[15,46,47,71,103,117,124,131],{"__ignoreMap":44},[48,49,52,56,60,64,68],"span",{"class":50,"line":51},"line",1,[48,53,55],{"class":54},"su5hD","text ",[48,57,59],{"class":58},"smGrS","=",[48,61,63],{"class":62},"sjJ54"," \"",[48,65,67],{"class":66},"s_sjI","hello world",[48,69,70],{"class":62},"\"\n",[48,72,74,77,79,82,85,89,92,95,98,100],{"class":50,"line":73},2,[48,75,76],{"class":54},"position ",[48,78,59],{"class":58},[48,80,81],{"class":54}," text",[48,83,32],{"class":84},"sP7_E",[48,86,88],{"class":87},"slqww","index",[48,90,91],{"class":84},"(",[48,93,94],{"class":62},"\"",[48,96,97],{"class":66},"world",[48,99,94],{"class":62},[48,101,102],{"class":84},")\n",[48,104,106,110,112,115],{"class":50,"line":105},3,[48,107,109],{"class":108},"sptTA","print",[48,111,91],{"class":84},[48,113,114],{"class":87},"position",[48,116,102],{"class":84},[48,118,120],{"class":50,"line":119},4,[48,121,123],{"emptyLinePlaceholder":122},true,"\n",[48,125,127],{"class":50,"line":126},5,[48,128,130],{"class":129},"sutJx","# Output:\n",[48,132,134],{"class":50,"line":133},6,[48,135,136],{"class":129},"# 6\n",[20,138,139,140,142,143,32],{},"Use ",[15,141,17],{}," when you want the position of a substring and expect it to exist. If the value is missing, Python raises a ",[15,144,31],{},[34,146,148,149,152],{"id":147},"what-strindex-does","What ",[15,150,151],{},"str.index()"," does",[20,154,155,157],{},[15,156,151],{}," searches a string and gives back the position of the first match.",[20,159,160],{},"Key points:",[162,163,164,168,171,174],"ul",{},[165,166,167],"li",{},"It returns the position of the first match in a string",[165,169,170],{},"It works with a single character or a longer substring",[165,172,173],{},"The search is case-sensitive",[165,175,176,177,179],{},"It raises ",[15,178,31],{}," if the substring is not found",[20,181,182],{},"Example:",[39,184,186],{"className":41,"code":185,"language":43,"meta":44,"style":44},"text = \"banana\"\n\nprint(text.index(\"a\"))     # 1\nprint(text.index(\"na\"))    # 2\n",[15,187,188,201,205,233],{"__ignoreMap":44},[48,189,190,192,194,196,199],{"class":50,"line":51},[48,191,55],{"class":54},[48,193,59],{"class":58},[48,195,63],{"class":62},[48,197,198],{"class":66},"banana",[48,200,70],{"class":62},[48,202,203],{"class":50,"line":73},[48,204,123],{"emptyLinePlaceholder":122},[48,206,207,209,211,214,216,218,220,222,225,227,230],{"class":50,"line":105},[48,208,109],{"class":108},[48,210,91],{"class":84},[48,212,213],{"class":87},"text",[48,215,32],{"class":84},[48,217,88],{"class":87},[48,219,91],{"class":84},[48,221,94],{"class":62},[48,223,224],{"class":66},"a",[48,226,94],{"class":62},[48,228,229],{"class":84},"))",[48,231,232],{"class":129},"     # 1\n",[48,234,235,237,239,241,243,245,247,249,252,254,256],{"class":50,"line":119},[48,236,109],{"class":108},[48,238,91],{"class":84},[48,240,213],{"class":87},[48,242,32],{"class":84},[48,244,88],{"class":87},[48,246,91],{"class":84},[48,248,94],{"class":62},[48,250,251],{"class":66},"na",[48,253,94],{"class":62},[48,255,229],{"class":84},[48,257,258],{"class":129},"    # 2\n",[20,260,261],{},"Here:",[162,263,264,273],{},[165,265,266,269,270],{},[15,267,268],{},"\"a\""," first appears at index ",[15,271,272],{},"1",[165,274,275,269,278],{},[15,276,277],{},"\"na\"",[15,279,280],{},"2",[20,282,283],{},"Case sensitivity matters:",[39,285,287],{"className":41,"code":286,"language":43,"meta":44,"style":44},"text = \"Hello\"\n\nprint(text.index(\"H\"))   # 0\n# print(text.index(\"h\")) # ValueError\n",[15,288,289,302,306,332],{"__ignoreMap":44},[48,290,291,293,295,297,300],{"class":50,"line":51},[48,292,55],{"class":54},[48,294,59],{"class":58},[48,296,63],{"class":62},[48,298,299],{"class":66},"Hello",[48,301,70],{"class":62},[48,303,304],{"class":50,"line":73},[48,305,123],{"emptyLinePlaceholder":122},[48,307,308,310,312,314,316,318,320,322,325,327,329],{"class":50,"line":105},[48,309,109],{"class":108},[48,311,91],{"class":84},[48,313,213],{"class":87},[48,315,32],{"class":84},[48,317,88],{"class":87},[48,319,91],{"class":84},[48,321,94],{"class":62},[48,323,324],{"class":66},"H",[48,326,94],{"class":62},[48,328,229],{"class":84},[48,330,331],{"class":129},"   # 0\n",[48,333,334],{"class":50,"line":119},[48,335,336],{"class":129},"# print(text.index(\"h\")) # ValueError\n",[34,338,340],{"id":339},"basic-syntax","Basic syntax",[20,342,343],{},"The syntax is:",[39,345,347],{"className":41,"code":346,"language":43,"meta":44,"style":44},"string.index(value, start, end)\n",[15,348,349],{"__ignoreMap":44},[48,350,351,354,356,358,360,363,366,369,371,374],{"class":50,"line":51},[48,352,353],{"class":54},"string",[48,355,32],{"class":84},[48,357,88],{"class":87},[48,359,91],{"class":84},[48,361,362],{"class":87},"value",[48,364,365],{"class":84},",",[48,367,368],{"class":87}," start",[48,370,365],{"class":84},[48,372,373],{"class":87}," end",[48,375,102],{"class":84},[20,377,378],{},"Parameters:",[162,380,381,386,392],{},[165,382,383,385],{},[15,384,362],{},": the text to search for",[165,387,388,391],{},[15,389,390],{},"start",": optional starting position",[165,393,394,397],{},[15,395,396],{},"end",": optional stopping position",[20,399,400],{},"You can use just the value:",[39,402,404],{"className":41,"code":403,"language":43,"meta":44,"style":44},"text = \"python\"\nprint(text.index(\"t\"))  # 2\n",[15,405,406,418],{"__ignoreMap":44},[48,407,408,410,412,414,416],{"class":50,"line":51},[48,409,55],{"class":54},[48,411,59],{"class":58},[48,413,63],{"class":62},[48,415,43],{"class":66},[48,417,70],{"class":62},[48,419,420,422,424,426,428,430,432,434,437,439,441],{"class":50,"line":73},[48,421,109],{"class":108},[48,423,91],{"class":84},[48,425,213],{"class":87},[48,427,32],{"class":84},[48,429,88],{"class":87},[48,431,91],{"class":84},[48,433,94],{"class":62},[48,435,436],{"class":66},"t",[48,438,94],{"class":62},[48,440,229],{"class":84},[48,442,443],{"class":129},"  # 2\n",[20,445,446],{},"Or include a start position:",[39,448,450],{"className":41,"code":449,"language":43,"meta":44,"style":44},"text = \"banana\"\nprint(text.index(\"a\", 2))  # 3\n",[15,451,452,464],{"__ignoreMap":44},[48,453,454,456,458,460,462],{"class":50,"line":51},[48,455,55],{"class":54},[48,457,59],{"class":58},[48,459,63],{"class":62},[48,461,198],{"class":66},[48,463,70],{"class":62},[48,465,466,468,470,472,474,476,478,480,482,484,486,490,492],{"class":50,"line":73},[48,467,109],{"class":108},[48,469,91],{"class":84},[48,471,213],{"class":87},[48,473,32],{"class":84},[48,475,88],{"class":87},[48,477,91],{"class":84},[48,479,94],{"class":62},[48,481,224],{"class":66},[48,483,94],{"class":62},[48,485,365],{"class":84},[48,487,489],{"class":488},"srdBf"," 2",[48,491,229],{"class":84},[48,493,494],{"class":129},"  # 3\n",[20,496,497,498,500,501,503],{},"Or include both ",[15,499,390],{}," and ",[15,502,396],{},":",[39,505,507],{"className":41,"code":506,"language":43,"meta":44,"style":44},"text = \"banana\"\nprint(text.index(\"a\", 2, 5))  # 3\n",[15,508,509,521],{"__ignoreMap":44},[48,510,511,513,515,517,519],{"class":50,"line":51},[48,512,55],{"class":54},[48,514,59],{"class":58},[48,516,63],{"class":62},[48,518,198],{"class":66},[48,520,70],{"class":62},[48,522,523,525,527,529,531,533,535,537,539,541,543,545,547,550,552],{"class":50,"line":73},[48,524,109],{"class":108},[48,526,91],{"class":84},[48,528,213],{"class":87},[48,530,32],{"class":84},[48,532,88],{"class":87},[48,534,91],{"class":84},[48,536,94],{"class":62},[48,538,224],{"class":66},[48,540,94],{"class":62},[48,542,365],{"class":84},[48,544,489],{"class":488},[48,546,365],{"class":84},[48,548,549],{"class":488}," 5",[48,551,229],{"class":84},[48,553,494],{"class":129},[34,555,557],{"id":556},"what-the-return-value-means","What the return value means",[20,559,560,561,563,564,32],{},"The value returned by ",[15,562,17],{}," is a ",[565,566,567],"strong",{},"zero-based index",[20,569,570],{},"That means:",[162,572,573,580,585],{},[165,574,575,576,579],{},"Index ",[15,577,578],{},"0"," is the first character",[165,581,575,582,584],{},[15,583,272],{}," is the second character",[165,586,575,587,589],{},[15,588,280],{}," is the third character",[20,591,182],{},[39,593,595],{"className":41,"code":594,"language":43,"meta":44,"style":44},"text = \"cat\"\n\nprint(text.index(\"c\"))  # 0\nprint(text.index(\"a\"))  # 1\nprint(text.index(\"t\"))  # 2\n",[15,596,597,610,614,640,665],{"__ignoreMap":44},[48,598,599,601,603,605,608],{"class":50,"line":51},[48,600,55],{"class":54},[48,602,59],{"class":58},[48,604,63],{"class":62},[48,606,607],{"class":66},"cat",[48,609,70],{"class":62},[48,611,612],{"class":50,"line":73},[48,613,123],{"emptyLinePlaceholder":122},[48,615,616,618,620,622,624,626,628,630,633,635,637],{"class":50,"line":105},[48,617,109],{"class":108},[48,619,91],{"class":84},[48,621,213],{"class":87},[48,623,32],{"class":84},[48,625,88],{"class":87},[48,627,91],{"class":84},[48,629,94],{"class":62},[48,631,632],{"class":66},"c",[48,634,94],{"class":62},[48,636,229],{"class":84},[48,638,639],{"class":129},"  # 0\n",[48,641,642,644,646,648,650,652,654,656,658,660,662],{"class":50,"line":119},[48,643,109],{"class":108},[48,645,91],{"class":84},[48,647,213],{"class":87},[48,649,32],{"class":84},[48,651,88],{"class":87},[48,653,91],{"class":84},[48,655,94],{"class":62},[48,657,224],{"class":66},[48,659,94],{"class":62},[48,661,229],{"class":84},[48,663,664],{"class":129},"  # 1\n",[48,666,667,669,671,673,675,677,679,681,683,685,687],{"class":50,"line":126},[48,668,109],{"class":108},[48,670,91],{"class":84},[48,672,213],{"class":87},[48,674,32],{"class":84},[48,676,88],{"class":87},[48,678,91],{"class":84},[48,680,94],{"class":62},[48,682,436],{"class":66},[48,684,94],{"class":62},[48,686,229],{"class":84},[48,688,443],{"class":129},[20,690,691,693,694,697],{},[15,692,17],{}," returns only the ",[565,695,696],{},"first"," matching position.",[39,699,701],{"className":41,"code":700,"language":43,"meta":44,"style":44},"text = \"banana\"\nprint(text.index(\"a\"))  # 1\n",[15,702,703,715],{"__ignoreMap":44},[48,704,705,707,709,711,713],{"class":50,"line":51},[48,706,55],{"class":54},[48,708,59],{"class":58},[48,710,63],{"class":62},[48,712,198],{"class":66},[48,714,70],{"class":62},[48,716,717,719,721,723,725,727,729,731,733,735,737],{"class":50,"line":73},[48,718,109],{"class":108},[48,720,91],{"class":84},[48,722,213],{"class":87},[48,724,32],{"class":84},[48,726,88],{"class":87},[48,728,91],{"class":84},[48,730,94],{"class":62},[48,732,224],{"class":66},[48,734,94],{"class":62},[48,736,229],{"class":84},[48,738,664],{"class":129},[20,740,741,742,744],{},"Even though ",[15,743,268],{}," appears more than once, the method returns only the first match.",[20,746,747,748,32],{},"If you need to count matches instead, see the ",[224,749,751,754],{"href":750},"\u002Freference\u002Fpython-string-count-method",[15,752,753],{},"str.count()"," method",[34,756,758,759,500,761],{"id":757},"using-start-and-end","Using ",[15,760,390],{},[15,762,396],{},[20,764,765,766,500,768,770],{},"The ",[15,767,390],{},[15,769,396],{}," arguments let you search only part of the string.",[20,772,773],{},"This is useful when the same text appears more than once.",[20,775,776,777,503],{},"Example with ",[15,778,390],{},[39,780,782],{"className":41,"code":781,"language":43,"meta":44,"style":44},"text = \"banana\"\n\nprint(text.index(\"a\"))      # 1\nprint(text.index(\"a\", 2))   # 3\nprint(text.index(\"a\", 4))   # 5\n",[15,783,784,796,800,825,854],{"__ignoreMap":44},[48,785,786,788,790,792,794],{"class":50,"line":51},[48,787,55],{"class":54},[48,789,59],{"class":58},[48,791,63],{"class":62},[48,793,198],{"class":66},[48,795,70],{"class":62},[48,797,798],{"class":50,"line":73},[48,799,123],{"emptyLinePlaceholder":122},[48,801,802,804,806,808,810,812,814,816,818,820,822],{"class":50,"line":105},[48,803,109],{"class":108},[48,805,91],{"class":84},[48,807,213],{"class":87},[48,809,32],{"class":84},[48,811,88],{"class":87},[48,813,91],{"class":84},[48,815,94],{"class":62},[48,817,224],{"class":66},[48,819,94],{"class":62},[48,821,229],{"class":84},[48,823,824],{"class":129},"      # 1\n",[48,826,827,829,831,833,835,837,839,841,843,845,847,849,851],{"class":50,"line":119},[48,828,109],{"class":108},[48,830,91],{"class":84},[48,832,213],{"class":87},[48,834,32],{"class":84},[48,836,88],{"class":87},[48,838,91],{"class":84},[48,840,94],{"class":62},[48,842,224],{"class":66},[48,844,94],{"class":62},[48,846,365],{"class":84},[48,848,489],{"class":488},[48,850,229],{"class":84},[48,852,853],{"class":129},"   # 3\n",[48,855,856,858,860,862,864,866,868,870,872,874,876,879,881],{"class":50,"line":126},[48,857,109],{"class":108},[48,859,91],{"class":84},[48,861,213],{"class":87},[48,863,32],{"class":84},[48,865,88],{"class":87},[48,867,91],{"class":84},[48,869,94],{"class":62},[48,871,224],{"class":66},[48,873,94],{"class":62},[48,875,365],{"class":84},[48,877,878],{"class":488}," 4",[48,880,229],{"class":84},[48,882,883],{"class":129},"   # 5\n",[20,885,886],{},"In this example:",[162,888,889,897,903],{},[165,890,891,892,894,895],{},"The first search finds the first ",[15,893,268],{}," at index ",[15,896,272],{},[165,898,899,900,902],{},"Starting at index ",[15,901,280],{}," skips that first match",[165,904,899,905,908,909],{},[15,906,907],{},"4"," finds the last ",[15,910,268],{},[20,912,776,913,503],{},[15,914,396],{},[39,916,918],{"className":41,"code":917,"language":43,"meta":44,"style":44},"text = \"banana\"\n\nprint(text.index(\"n\", 0, 4))  # 2\n",[15,919,920,932,936],{"__ignoreMap":44},[48,921,922,924,926,928,930],{"class":50,"line":51},[48,923,55],{"class":54},[48,925,59],{"class":58},[48,927,63],{"class":62},[48,929,198],{"class":66},[48,931,70],{"class":62},[48,933,934],{"class":50,"line":73},[48,935,123],{"emptyLinePlaceholder":122},[48,937,938,940,942,944,946,948,950,952,955,957,959,962,964,966,968],{"class":50,"line":105},[48,939,109],{"class":108},[48,941,91],{"class":84},[48,943,213],{"class":87},[48,945,32],{"class":84},[48,947,88],{"class":87},[48,949,91],{"class":84},[48,951,94],{"class":62},[48,953,954],{"class":66},"n",[48,956,94],{"class":62},[48,958,365],{"class":84},[48,960,961],{"class":488}," 0",[48,963,365],{"class":84},[48,965,878],{"class":488},[48,967,229],{"class":84},[48,969,443],{"class":129},[20,971,972],{},"This searches only within part of the string.",[20,974,975],{},"One important detail: the returned index still refers to the original string, not a smaller temporary section.",[34,977,979,980,982],{"id":978},"when-index-causes-an-error","When ",[15,981,17],{}," causes an error",[20,984,985,986,988,989,32],{},"If the substring is not found, ",[15,987,17],{}," raises a ",[15,990,31],{},[20,992,182],{},[39,994,996],{"className":41,"code":995,"language":43,"meta":44,"style":44},"text = \"hello\"\nprint(text.index(\"z\"))\n",[15,997,998,1011],{"__ignoreMap":44},[48,999,1000,1002,1004,1006,1009],{"class":50,"line":51},[48,1001,55],{"class":54},[48,1003,59],{"class":58},[48,1005,63],{"class":62},[48,1007,1008],{"class":66},"hello",[48,1010,70],{"class":62},[48,1012,1013,1015,1017,1019,1021,1023,1025,1027,1030,1032],{"class":50,"line":73},[48,1014,109],{"class":108},[48,1016,91],{"class":84},[48,1018,213],{"class":87},[48,1020,32],{"class":84},[48,1022,88],{"class":87},[48,1024,91],{"class":84},[48,1026,94],{"class":62},[48,1028,1029],{"class":66},"z",[48,1031,94],{"class":62},[48,1033,1034],{"class":84},"))\n",[20,1036,1037],{},"Output:",[39,1039,1041],{"className":41,"code":1040,"language":43,"meta":44,"style":44},"ValueError: substring not found\n",[15,1042,1043],{"__ignoreMap":44},[48,1044,1045,1048,1050,1053,1056],{"class":50,"line":51},[48,1046,31],{"class":1047},"sZMiF",[48,1049,503],{"class":84},[48,1051,1052],{"class":54}," substring ",[48,1054,1055],{"class":58},"not",[48,1057,1058],{"class":54}," found\n",[20,1060,1061,1062,1068,1069,1072],{},"This is different from ",[224,1063,1065],{"href":1064},"\u002Freference\u002Fpython-string-find-method",[15,1066,1067],{},"str.find()",", which returns ",[15,1070,1071],{},"-1"," instead of raising an error.",[20,1074,1075,1076,500,1079,503],{},"If the value may not exist, use ",[15,1077,1078],{},"try",[15,1080,1081],{},"except",[39,1083,1085],{"className":41,"code":1084,"language":43,"meta":44,"style":44},"text = \"hello\"\n\ntry:\n    position = text.index(\"z\")\n    print(position)\nexcept ValueError:\n    print(\"Substring not found\")\n",[15,1086,1087,1099,1103,1111,1134,1145,1154],{"__ignoreMap":44},[48,1088,1089,1091,1093,1095,1097],{"class":50,"line":51},[48,1090,55],{"class":54},[48,1092,59],{"class":58},[48,1094,63],{"class":62},[48,1096,1008],{"class":66},[48,1098,70],{"class":62},[48,1100,1101],{"class":50,"line":73},[48,1102,123],{"emptyLinePlaceholder":122},[48,1104,1105,1108],{"class":50,"line":105},[48,1106,1078],{"class":1107},"sVHd0",[48,1109,1110],{"class":84},":\n",[48,1112,1113,1116,1118,1120,1122,1124,1126,1128,1130,1132],{"class":50,"line":119},[48,1114,1115],{"class":54},"    position ",[48,1117,59],{"class":58},[48,1119,81],{"class":54},[48,1121,32],{"class":84},[48,1123,88],{"class":87},[48,1125,91],{"class":84},[48,1127,94],{"class":62},[48,1129,1029],{"class":66},[48,1131,94],{"class":62},[48,1133,102],{"class":84},[48,1135,1136,1139,1141,1143],{"class":50,"line":126},[48,1137,1138],{"class":108},"    print",[48,1140,91],{"class":84},[48,1142,114],{"class":87},[48,1144,102],{"class":84},[48,1146,1147,1149,1152],{"class":50,"line":133},[48,1148,1081],{"class":1107},[48,1150,1151],{"class":1047}," ValueError",[48,1153,1110],{"class":84},[48,1155,1157,1159,1161,1163,1166,1168],{"class":50,"line":1156},7,[48,1158,1138],{"class":108},[48,1160,91],{"class":84},[48,1162,94],{"class":62},[48,1164,1165],{"class":66},"Substring not found",[48,1167,94],{"class":62},[48,1169,102],{"class":84},[20,1171,1172],{},"This helps your program continue running.",[20,1174,1175,1176,32],{},"If you are dealing with this exact error, see ",[224,1177,1179,1180],{"href":1178},"\u002Ferrors\u002Fvalueerror-substring-not-found-fix","how to fix ",[15,1181,1182],{},"ValueError: substring not found",[34,1184,1186,1188,1189],{"id":1185},"index-vs-find",[15,1187,17],{}," vs ",[15,1190,1191],{},"find()",[20,1193,1194,500,1196,1198],{},[15,1195,17],{},[15,1197,1191],{}," are very similar, but they behave differently when no match is found.",[1200,1201,1202],"h3",{"id":88},[15,1203,17],{},[162,1205,1206,1209],{},[165,1207,1208],{},"Returns the first match position",[165,1210,1211],{},"Raises an error if nothing is found",[39,1213,1215],{"className":41,"code":1214,"language":43,"meta":44,"style":44},"text = \"hello\"\nprint(text.index(\"e\"))  # 1\n# print(text.index(\"z\"))  # ValueError\n",[15,1216,1217,1229,1254],{"__ignoreMap":44},[48,1218,1219,1221,1223,1225,1227],{"class":50,"line":51},[48,1220,55],{"class":54},[48,1222,59],{"class":58},[48,1224,63],{"class":62},[48,1226,1008],{"class":66},[48,1228,70],{"class":62},[48,1230,1231,1233,1235,1237,1239,1241,1243,1245,1248,1250,1252],{"class":50,"line":73},[48,1232,109],{"class":108},[48,1234,91],{"class":84},[48,1236,213],{"class":87},[48,1238,32],{"class":84},[48,1240,88],{"class":87},[48,1242,91],{"class":84},[48,1244,94],{"class":62},[48,1246,1247],{"class":66},"e",[48,1249,94],{"class":62},[48,1251,229],{"class":84},[48,1253,664],{"class":129},[48,1255,1256],{"class":50,"line":105},[48,1257,1258],{"class":129},"# print(text.index(\"z\"))  # ValueError\n",[1200,1260,1262],{"id":1261},"find",[15,1263,1191],{},[162,1265,1266,1268],{},[165,1267,1208],{},[165,1269,1270,1271,1273],{},"Returns ",[15,1272,1071],{}," if nothing is found",[39,1275,1277],{"className":41,"code":1276,"language":43,"meta":44,"style":44},"text = \"hello\"\nprint(text.find(\"e\"))  # 1\nprint(text.find(\"z\"))  # -1\n",[15,1278,1279,1291,1315],{"__ignoreMap":44},[48,1280,1281,1283,1285,1287,1289],{"class":50,"line":51},[48,1282,55],{"class":54},[48,1284,59],{"class":58},[48,1286,63],{"class":62},[48,1288,1008],{"class":66},[48,1290,70],{"class":62},[48,1292,1293,1295,1297,1299,1301,1303,1305,1307,1309,1311,1313],{"class":50,"line":73},[48,1294,109],{"class":108},[48,1296,91],{"class":84},[48,1298,213],{"class":87},[48,1300,32],{"class":84},[48,1302,1261],{"class":87},[48,1304,91],{"class":84},[48,1306,94],{"class":62},[48,1308,1247],{"class":66},[48,1310,94],{"class":62},[48,1312,229],{"class":84},[48,1314,664],{"class":129},[48,1316,1317,1319,1321,1323,1325,1327,1329,1331,1333,1335,1337],{"class":50,"line":105},[48,1318,109],{"class":108},[48,1320,91],{"class":84},[48,1322,213],{"class":87},[48,1324,32],{"class":84},[48,1326,1261],{"class":87},[48,1328,91],{"class":84},[48,1330,94],{"class":62},[48,1332,1029],{"class":66},[48,1334,94],{"class":62},[48,1336,229],{"class":84},[48,1338,1339],{"class":129},"  # -1\n",[20,1341,139,1342,1344],{},[15,1343,17],{}," when missing data should be treated as a problem.",[20,1346,139,1347,1349],{},[15,1348,1191],{}," when you want to check safely without an exception.",[20,1351,1352,1353,1358,1359,32],{},"For a full comparison, see the ",[224,1354,1355,1357],{"href":1064},[15,1356,1067],{}," method reference"," or ",[224,1360,1362],{"href":1361},"\u002Fhow-to\u002Fhow-to-check-if-a-string-contains-a-substring-in-python","how to check if a string contains a substring in Python",[34,1364,1366],{"id":1365},"common-mistakes","Common mistakes",[20,1368,1369,1370,503],{},"Beginners often run into these problems when using ",[15,1371,17],{},[162,1373,1374,1377,1386,1391,1396],{},[165,1375,1376],{},"Searching for text with the wrong uppercase or lowercase letters",[165,1378,1379,1380,1382,1383,1385],{},"Expecting ",[15,1381,17],{}," to return ",[15,1384,1071],{}," when no match exists",[165,1387,758,1388,1390],{},[15,1389,17],{}," on a value that may not be present",[165,1392,1393,1394],{},"Confusing character position with human counting starting at ",[15,1395,272],{},[165,1397,1398,1399,1401],{},"Assuming ",[15,1400,17],{}," returns every match instead of the first one",[20,1403,1404],{},"Useful checks while debugging:",[39,1406,1408],{"className":41,"code":1407,"language":43,"meta":44,"style":44},"print(text)\nprint(repr(text))\nprint(text.index(\"value\"))\nprint(text.find(\"value\"))\nprint(text.index(\"value\", start, end))\n",[15,1409,1410,1420,1435,1457,1479],{"__ignoreMap":44},[48,1411,1412,1414,1416,1418],{"class":50,"line":51},[48,1413,109],{"class":108},[48,1415,91],{"class":84},[48,1417,213],{"class":87},[48,1419,102],{"class":84},[48,1421,1422,1424,1426,1429,1431,1433],{"class":50,"line":73},[48,1423,109],{"class":108},[48,1425,91],{"class":84},[48,1427,1428],{"class":108},"repr",[48,1430,91],{"class":84},[48,1432,213],{"class":87},[48,1434,1034],{"class":84},[48,1436,1437,1439,1441,1443,1445,1447,1449,1451,1453,1455],{"class":50,"line":105},[48,1438,109],{"class":108},[48,1440,91],{"class":84},[48,1442,213],{"class":87},[48,1444,32],{"class":84},[48,1446,88],{"class":87},[48,1448,91],{"class":84},[48,1450,94],{"class":62},[48,1452,362],{"class":66},[48,1454,94],{"class":62},[48,1456,1034],{"class":84},[48,1458,1459,1461,1463,1465,1467,1469,1471,1473,1475,1477],{"class":50,"line":119},[48,1460,109],{"class":108},[48,1462,91],{"class":84},[48,1464,213],{"class":87},[48,1466,32],{"class":84},[48,1468,1261],{"class":87},[48,1470,91],{"class":84},[48,1472,94],{"class":62},[48,1474,362],{"class":66},[48,1476,94],{"class":62},[48,1478,1034],{"class":84},[48,1480,1481,1483,1485,1487,1489,1491,1493,1495,1497,1499,1501,1503,1505,1507],{"class":50,"line":126},[48,1482,109],{"class":108},[48,1484,91],{"class":84},[48,1486,213],{"class":87},[48,1488,32],{"class":84},[48,1490,88],{"class":87},[48,1492,91],{"class":84},[48,1494,94],{"class":62},[48,1496,362],{"class":66},[48,1498,94],{"class":62},[48,1500,365],{"class":84},[48,1502,368],{"class":87},[48,1504,365],{"class":84},[48,1506,373],{"class":87},[48,1508,1034],{"class":84},[20,1510,1511],{},"Tips:",[162,1513,1514,1520,1525,1528],{},[165,1515,139,1516,1519],{},[15,1517,1518],{},"print(repr(text))"," to spot spaces or hidden characters",[165,1521,139,1522,1524],{},[15,1523,1191],{}," if you want to test first without raising an error",[165,1526,1527],{},"Double-check uppercase and lowercase letters",[165,1529,1530,1531],{},"Remember that Python starts counting at ",[15,1532,578],{},[34,1534,1536],{"id":1535},"faq","FAQ",[1200,1538,1540,1541,1543],{"id":1539},"what-does-python-string-index-return","What does Python string ",[15,1542,17],{}," return?",[20,1545,1546],{},"It returns the zero-based position of the first match in the string.",[1200,1548,1550],{"id":1549},"what-happens-if-the-substring-is-not-found","What happens if the substring is not found?",[20,1552,1553,1554,32],{},"Python raises a ",[15,1555,31],{},[1200,1557,1559,1560,500,1562,1564],{"id":1558},"what-is-the-difference-between-index-and-find","What is the difference between ",[15,1561,17],{},[15,1563,1191],{},"?",[20,1566,1567,1569,1570,1572,1573,1575],{},[15,1568,17],{}," raises an error if nothing is found. ",[15,1571,1191],{}," returns ",[15,1574,1071],{}," instead.",[1200,1577,1579,1580,1582],{"id":1578},"can-index-search-for-more-than-one-character","Can ",[15,1581,17],{}," search for more than one character?",[20,1584,1585],{},"Yes. It can search for a substring, not just a single character.",[1200,1587,1589,1590,1592],{"id":1588},"is-string-index-case-sensitive","Is string ",[15,1591,17],{}," case-sensitive?",[20,1594,1595],{},"Yes. Uppercase and lowercase letters are treated as different.",[34,1597,1599],{"id":1598},"see-also","See also",[162,1601,1602,1608,1613,1619,1625],{},[165,1603,1604],{},[224,1605,1606,754],{"href":1064},[15,1607,1067],{},[165,1609,1610],{},[224,1611,1612],{"href":1361},"How to check if a string contains a substring in Python",[165,1614,1615],{},[224,1616,1618],{"href":1617},"\u002Fhow-to\u002Fhow-to-split-a-string-in-python","How to split a string in Python",[165,1620,1621],{},[224,1622,1623,754],{"href":750},[15,1624,753],{},[165,1626,1627],{},[224,1628,1629,1630],{"href":1178},"Fix ",[15,1631,1182],{},[1633,1634,1635],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}",{"title":44,"searchDepth":73,"depth":73,"links":1637},[1638,1639,1641,1642,1643,1645,1647,1652,1653,1664],{"id":36,"depth":73,"text":37},{"id":147,"depth":73,"text":1640},"What str.index() does",{"id":339,"depth":73,"text":340},{"id":556,"depth":73,"text":557},{"id":757,"depth":73,"text":1644},"Using start and end",{"id":978,"depth":73,"text":1646},"When index() causes an error",{"id":1185,"depth":73,"text":1648,"children":1649},"index() vs find()",[1650,1651],{"id":88,"depth":105,"text":17},{"id":1261,"depth":105,"text":1191},{"id":1365,"depth":73,"text":1366},{"id":1535,"depth":73,"text":1536,"children":1654},[1655,1657,1658,1660,1662],{"id":1539,"depth":105,"text":1656},"What does Python string index() return?",{"id":1549,"depth":105,"text":1550},{"id":1558,"depth":105,"text":1659},"What is the difference between index() and find()?",{"id":1578,"depth":105,"text":1661},"Can index() search for more than one character?",{"id":1588,"depth":105,"text":1663},"Is string index() case-sensitive?",{"id":1598,"depth":73,"text":1599},"Master python string index method in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-string-index-method",{"title":5,"description":1665},"reference\u002Fpython-string-index-method","R1ztM7XUbXLB6x68q72-dPTbtnrYXmbHumYw459ZPYA",1777585512787]