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