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