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