[{"data":1,"prerenderedAt":2520},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-replace-text-in-a-string-in-python":3},{"id":4,"title":5,"body":6,"description":2513,"extension":2514,"meta":2515,"navigation":286,"path":2516,"seo":2517,"stem":2518,"__hash__":2519},"content\u002Fhow-to\u002Fhow-to-replace-text-in-a-string-in-python.md","How to Replace Text in a String in Python",{"type":7,"value":8,"toc":2484},"minimark",[9,13,22,25,41,44,49,138,141,150,157,161,164,178,184,187,213,229,232,299,301,310,313,330,338,342,349,413,415,424,427,430,444,447,506,508,517,520,584,586,611,615,618,646,657,660,729,731,744,747,816,818,827,830,834,842,849,906,908,917,920,923,984,986,994,997,1058,1060,1068,1080,1084,1089,1099,1161,1163,1176,1182,1185,1199,1206,1240,1242,1259,1262,1269,1276,1281,1284,1311,1313,1373,1375,1391,1396,1399,1402,1464,1466,1481,1488,1503,1507,1513,1518,1578,1580,1589,1592,1653,1660,1663,1666,1676,1680,1728,1730,1739,1749,1753,1802,1804,1813,1816,1820,1823,1873,1875,1884,1889,1940,1942,1951,1955,1958,2057,2060,2095,2097,2212,2214,2261,2265,2272,2275,2279,2282,2309,2313,2316,2326,2330,2336,2339,2433,2435,2444,2448,2480],[10,11,5],"h1",{"id":12},"how-to-replace-text-in-a-string-in-python",[14,15,16,17,21],"p",{},"If you want to change part of a string in Python, the simplest tool is ",[18,19,20],"code",{},"str.replace()",".",[14,23,24],{},"It works well for common tasks like:",[26,27,28,32,35,38],"ul",{},[29,30,31],"li",{},"changing one word to another",[29,33,34],{},"removing part of a string",[29,36,37],{},"replacing all matches",[29,39,40],{},"replacing only the first few matches",[14,42,43],{},"For most beginner cases, this is the right method to use.",[45,46,48],"h2",{"id":47},"quick-answer","Quick answer",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","text = \"Hello world\"\nnew_text = text.replace(\"world\", \"Python\")\nprint(new_text)\n","python","",[18,57,58,82,124],{"__ignoreMap":55},[59,60,63,67,71,75,79],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"su5hD","text ",[59,68,70],{"class":69},"smGrS","=",[59,72,74],{"class":73},"sjJ54"," \"",[59,76,78],{"class":77},"s_sjI","Hello world",[59,80,81],{"class":73},"\"\n",[59,83,85,88,90,93,96,100,103,106,109,111,114,116,119,121],{"class":61,"line":84},2,[59,86,87],{"class":65},"new_text ",[59,89,70],{"class":69},[59,91,92],{"class":65}," text",[59,94,21],{"class":95},"sP7_E",[59,97,99],{"class":98},"slqww","replace",[59,101,102],{"class":95},"(",[59,104,105],{"class":73},"\"",[59,107,108],{"class":77},"world",[59,110,105],{"class":73},[59,112,113],{"class":95},",",[59,115,74],{"class":73},[59,117,118],{"class":77},"Python",[59,120,105],{"class":73},[59,122,123],{"class":95},")\n",[59,125,127,131,133,136],{"class":61,"line":126},3,[59,128,130],{"class":129},"sptTA","print",[59,132,102],{"class":95},[59,134,135],{"class":98},"new_text",[59,137,123],{"class":95},[14,139,140],{},"Output:",[50,142,144],{"className":52,"code":143,"language":54,"meta":55,"style":55},"Hello Python\n",[18,145,146],{"__ignoreMap":55},[59,147,148],{"class":61,"line":62},[59,149,143],{"class":65},[14,151,152,153,156],{},"Use ",[18,154,155],{},"str.replace(old, new)"," to replace text in a string. It returns a new string and does not change the original.",[45,158,160],{"id":159},"what-this-page-helps-you-do","What this page helps you do",[14,162,163],{},"This page shows you how to:",[26,165,166,169,172,175],{},[29,167,168],{},"Replace one word or phrase inside a string",[29,170,171],{},"Replace all matching parts of a string",[29,173,174],{},"Replace only the first few matches when needed",[29,176,177],{},"Understand that strings are immutable in Python",[45,179,152,181,183],{"id":180},"use-strreplace-for-most-text-replacement",[18,182,20],{}," for most text replacement",[14,185,186],{},"The basic syntax is:",[50,188,190],{"className":52,"code":189,"language":54,"meta":55,"style":55},"text.replace(old, new)\n",[18,191,192],{"__ignoreMap":55},[59,193,194,197,199,201,203,206,208,211],{"class":61,"line":62},[59,195,196],{"class":65},"text",[59,198,21],{"class":95},[59,200,99],{"class":98},[59,202,102],{"class":95},[59,204,205],{"class":98},"old",[59,207,113],{"class":95},[59,209,210],{"class":98}," new",[59,212,123],{"class":95},[26,214,215,220,226],{},[29,216,217,219],{},[18,218,205],{}," is the text to find",[29,221,222,225],{},[18,223,224],{},"new"," is the text to insert instead",[29,227,228],{},"the method returns a new string",[14,230,231],{},"Here is a simple example:",[50,233,235],{"className":52,"code":234,"language":54,"meta":55,"style":55},"text = \"I like cats\"\nnew_text = text.replace(\"cats\", \"dogs\")\n\nprint(new_text)\n",[18,236,237,250,282,288],{"__ignoreMap":55},[59,238,239,241,243,245,248],{"class":61,"line":62},[59,240,66],{"class":65},[59,242,70],{"class":69},[59,244,74],{"class":73},[59,246,247],{"class":77},"I like cats",[59,249,81],{"class":73},[59,251,252,254,256,258,260,262,264,266,269,271,273,275,278,280],{"class":61,"line":84},[59,253,87],{"class":65},[59,255,70],{"class":69},[59,257,92],{"class":65},[59,259,21],{"class":95},[59,261,99],{"class":98},[59,263,102],{"class":95},[59,265,105],{"class":73},[59,267,268],{"class":77},"cats",[59,270,105],{"class":73},[59,272,113],{"class":95},[59,274,74],{"class":73},[59,276,277],{"class":77},"dogs",[59,279,105],{"class":73},[59,281,123],{"class":95},[59,283,284],{"class":61,"line":126},[59,285,287],{"emptyLinePlaceholder":286},true,"\n",[59,289,291,293,295,297],{"class":61,"line":290},4,[59,292,130],{"class":129},[59,294,102],{"class":95},[59,296,135],{"class":98},[59,298,123],{"class":95},[14,300,140],{},[50,302,304],{"className":52,"code":303,"language":54,"meta":55,"style":55},"I like dogs\n",[18,305,306],{"__ignoreMap":55},[59,307,308],{"class":61,"line":62},[59,309,303],{"class":65},[14,311,312],{},"In this example:",[26,314,315,321,327],{},[29,316,317,318],{},"Python looks for ",[18,319,320],{},"\"cats\"",[29,322,323,324],{},"it replaces it with ",[18,325,326],{},"\"dogs\"",[29,328,329],{},"it gives back a new string",[14,331,332,333,21],{},"If you are new to strings, see ",[334,335,337],"a",{"href":336},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples","Python strings explained",[45,339,341],{"id":340},"replace-all-matches","Replace all matches",[14,343,344,345,348],{},"By default, ",[18,346,347],{},"replace()"," changes every matching occurrence.",[50,350,352],{"className":52,"code":351,"language":54,"meta":55,"style":55},"text = \"red blue red green red\"\nnew_text = text.replace(\"red\", \"yellow\")\n\nprint(new_text)\n",[18,353,354,367,399,403],{"__ignoreMap":55},[59,355,356,358,360,362,365],{"class":61,"line":62},[59,357,66],{"class":65},[59,359,70],{"class":69},[59,361,74],{"class":73},[59,363,364],{"class":77},"red blue red green red",[59,366,81],{"class":73},[59,368,369,371,373,375,377,379,381,383,386,388,390,392,395,397],{"class":61,"line":84},[59,370,87],{"class":65},[59,372,70],{"class":69},[59,374,92],{"class":65},[59,376,21],{"class":95},[59,378,99],{"class":98},[59,380,102],{"class":95},[59,382,105],{"class":73},[59,384,385],{"class":77},"red",[59,387,105],{"class":73},[59,389,113],{"class":95},[59,391,74],{"class":73},[59,393,394],{"class":77},"yellow",[59,396,105],{"class":73},[59,398,123],{"class":95},[59,400,401],{"class":61,"line":126},[59,402,287],{"emptyLinePlaceholder":286},[59,404,405,407,409,411],{"class":61,"line":290},[59,406,130],{"class":129},[59,408,102],{"class":95},[59,410,135],{"class":98},[59,412,123],{"class":95},[14,414,140],{},[50,416,418],{"className":52,"code":417,"language":54,"meta":55,"style":55},"yellow blue yellow green yellow\n",[18,419,420],{"__ignoreMap":55},[59,421,422],{"class":61,"line":62},[59,423,417],{"class":65},[14,425,426],{},"This is useful for simple cleanup and updates.",[14,428,429],{},"You can replace:",[26,431,432,435,438,441],{},[29,433,434],{},"words",[29,436,437],{},"spaces",[29,439,440],{},"symbols",[29,442,443],{},"short phrases",[14,445,446],{},"Example with spaces:",[50,448,450],{"className":52,"code":449,"language":54,"meta":55,"style":55},"text = \"apple    banana    orange\"\nnew_text = text.replace(\"    \", \" \")\n\nprint(new_text)\n",[18,451,452,465,492,496],{"__ignoreMap":55},[59,453,454,456,458,460,463],{"class":61,"line":62},[59,455,66],{"class":65},[59,457,70],{"class":69},[59,459,74],{"class":73},[59,461,462],{"class":77},"apple    banana    orange",[59,464,81],{"class":73},[59,466,467,469,471,473,475,477,479,481,484,486,488,490],{"class":61,"line":84},[59,468,87],{"class":65},[59,470,70],{"class":69},[59,472,92],{"class":65},[59,474,21],{"class":95},[59,476,99],{"class":98},[59,478,102],{"class":95},[59,480,105],{"class":73},[59,482,483],{"class":73},"    \"",[59,485,113],{"class":95},[59,487,74],{"class":73},[59,489,74],{"class":73},[59,491,123],{"class":95},[59,493,494],{"class":61,"line":126},[59,495,287],{"emptyLinePlaceholder":286},[59,497,498,500,502,504],{"class":61,"line":290},[59,499,130],{"class":129},[59,501,102],{"class":95},[59,503,135],{"class":98},[59,505,123],{"class":95},[14,507,140],{},[50,509,511],{"className":52,"code":510,"language":54,"meta":55,"style":55},"apple banana orange\n",[18,512,513],{"__ignoreMap":55},[59,514,515],{"class":61,"line":62},[59,516,510],{"class":65},[14,518,519],{},"Example with symbols:",[50,521,523],{"className":52,"code":522,"language":54,"meta":55,"style":55},"text = \"2024\u002F01\u002F15\"\nnew_text = text.replace(\"\u002F\", \"-\")\n\nprint(new_text)\n",[18,524,525,538,570,574],{"__ignoreMap":55},[59,526,527,529,531,533,536],{"class":61,"line":62},[59,528,66],{"class":65},[59,530,70],{"class":69},[59,532,74],{"class":73},[59,534,535],{"class":77},"2024\u002F01\u002F15",[59,537,81],{"class":73},[59,539,540,542,544,546,548,550,552,554,557,559,561,563,566,568],{"class":61,"line":84},[59,541,87],{"class":65},[59,543,70],{"class":69},[59,545,92],{"class":65},[59,547,21],{"class":95},[59,549,99],{"class":98},[59,551,102],{"class":95},[59,553,105],{"class":73},[59,555,556],{"class":77},"\u002F",[59,558,105],{"class":73},[59,560,113],{"class":95},[59,562,74],{"class":73},[59,564,565],{"class":77},"-",[59,567,105],{"class":73},[59,569,123],{"class":95},[59,571,572],{"class":61,"line":126},[59,573,287],{"emptyLinePlaceholder":286},[59,575,576,578,580,582],{"class":61,"line":290},[59,577,130],{"class":129},[59,579,102],{"class":95},[59,581,135],{"class":98},[59,583,123],{"class":95},[14,585,140],{},[50,587,589],{"className":52,"code":588,"language":54,"meta":55,"style":55},"2024-01-15\n",[18,590,591],{"__ignoreMap":55},[59,592,593,597,599,602,606,608],{"class":61,"line":62},[59,594,596],{"class":595},"srdBf","2024",[59,598,565],{"class":69},[59,600,601],{"class":595},"0",[59,603,605],{"class":604},"sx12J","1",[59,607,565],{"class":69},[59,609,610],{"class":595},"15\n",[45,612,614],{"id":613},"replace-only-a-limited-number-of-matches","Replace only a limited number of matches",[14,616,617],{},"If you only want to replace some matches, use the third argument:",[50,619,621],{"className":52,"code":620,"language":54,"meta":55,"style":55},"text.replace(old, new, count)\n",[18,622,623],{"__ignoreMap":55},[59,624,625,627,629,631,633,635,637,639,641,644],{"class":61,"line":62},[59,626,196],{"class":65},[59,628,21],{"class":95},[59,630,99],{"class":98},[59,632,102],{"class":95},[59,634,205],{"class":98},[59,636,113],{"class":95},[59,638,210],{"class":98},[59,640,113],{"class":95},[59,642,643],{"class":98}," count",[59,645,123],{"class":95},[26,647,648,654],{},[29,649,650,653],{},[18,651,652],{},"count"," tells Python how many matches to replace",[29,655,656],{},"replacement happens from left to right",[14,658,659],{},"Example:",[50,661,663],{"className":52,"code":662,"language":54,"meta":55,"style":55},"text = \"one two one two one\"\nnew_text = text.replace(\"one\", \"ONE\", 1)\n\nprint(new_text)\n",[18,664,665,678,715,719],{"__ignoreMap":55},[59,666,667,669,671,673,676],{"class":61,"line":62},[59,668,66],{"class":65},[59,670,70],{"class":69},[59,672,74],{"class":73},[59,674,675],{"class":77},"one two one two one",[59,677,81],{"class":73},[59,679,680,682,684,686,688,690,692,694,697,699,701,703,706,708,710,713],{"class":61,"line":84},[59,681,87],{"class":65},[59,683,70],{"class":69},[59,685,92],{"class":65},[59,687,21],{"class":95},[59,689,99],{"class":98},[59,691,102],{"class":95},[59,693,105],{"class":73},[59,695,696],{"class":77},"one",[59,698,105],{"class":73},[59,700,113],{"class":95},[59,702,74],{"class":73},[59,704,705],{"class":77},"ONE",[59,707,105],{"class":73},[59,709,113],{"class":95},[59,711,712],{"class":595}," 1",[59,714,123],{"class":95},[59,716,717],{"class":61,"line":126},[59,718,287],{"emptyLinePlaceholder":286},[59,720,721,723,725,727],{"class":61,"line":290},[59,722,130],{"class":129},[59,724,102],{"class":95},[59,726,135],{"class":98},[59,728,123],{"class":95},[14,730,140],{},[50,732,734],{"className":52,"code":733,"language":54,"meta":55,"style":55},"ONE two one two one\n",[18,735,736],{"__ignoreMap":55},[59,737,738,741],{"class":61,"line":62},[59,739,705],{"class":740},"s_hVV",[59,742,743],{"class":65}," two one two one\n",[14,745,746],{},"Replace the first two matches:",[50,748,750],{"className":52,"code":749,"language":54,"meta":55,"style":55},"text = \"cat cat cat\"\nnew_text = text.replace(\"cat\", \"dog\", 2)\n\nprint(new_text)\n",[18,751,752,765,802,806],{"__ignoreMap":55},[59,753,754,756,758,760,763],{"class":61,"line":62},[59,755,66],{"class":65},[59,757,70],{"class":69},[59,759,74],{"class":73},[59,761,762],{"class":77},"cat cat cat",[59,764,81],{"class":73},[59,766,767,769,771,773,775,777,779,781,784,786,788,790,793,795,797,800],{"class":61,"line":84},[59,768,87],{"class":65},[59,770,70],{"class":69},[59,772,92],{"class":65},[59,774,21],{"class":95},[59,776,99],{"class":98},[59,778,102],{"class":95},[59,780,105],{"class":73},[59,782,783],{"class":77},"cat",[59,785,105],{"class":73},[59,787,113],{"class":95},[59,789,74],{"class":73},[59,791,792],{"class":77},"dog",[59,794,105],{"class":73},[59,796,113],{"class":95},[59,798,799],{"class":595}," 2",[59,801,123],{"class":95},[59,803,804],{"class":61,"line":126},[59,805,287],{"emptyLinePlaceholder":286},[59,807,808,810,812,814],{"class":61,"line":290},[59,809,130],{"class":129},[59,811,102],{"class":95},[59,813,135],{"class":98},[59,815,123],{"class":95},[14,817,140],{},[50,819,821],{"className":52,"code":820,"language":54,"meta":55,"style":55},"dog dog cat\n",[18,822,823],{"__ignoreMap":55},[59,824,825],{"class":61,"line":62},[59,826,820],{"class":65},[14,828,829],{},"This is helpful when you only want to change the first occurrence instead of every match.",[45,831,833],{"id":832},"strings-do-not-change-in-place","Strings do not change in place",[14,835,836,837,841],{},"Strings are ",[838,839,840],"strong",{},"immutable"," in Python. That means they cannot be edited directly.",[14,843,844,845,848],{},"So this does ",[838,846,847],{},"not"," change the original string unless you save the result:",[50,850,852],{"className":52,"code":851,"language":54,"meta":55,"style":55},"text = \"Hello world\"\ntext.replace(\"world\", \"Python\")\n\nprint(text)\n",[18,853,854,866,892,896],{"__ignoreMap":55},[59,855,856,858,860,862,864],{"class":61,"line":62},[59,857,66],{"class":65},[59,859,70],{"class":69},[59,861,74],{"class":73},[59,863,78],{"class":77},[59,865,81],{"class":73},[59,867,868,870,872,874,876,878,880,882,884,886,888,890],{"class":61,"line":84},[59,869,196],{"class":65},[59,871,21],{"class":95},[59,873,99],{"class":98},[59,875,102],{"class":95},[59,877,105],{"class":73},[59,879,108],{"class":77},[59,881,105],{"class":73},[59,883,113],{"class":95},[59,885,74],{"class":73},[59,887,118],{"class":77},[59,889,105],{"class":73},[59,891,123],{"class":95},[59,893,894],{"class":61,"line":126},[59,895,287],{"emptyLinePlaceholder":286},[59,897,898,900,902,904],{"class":61,"line":290},[59,899,130],{"class":129},[59,901,102],{"class":95},[59,903,196],{"class":98},[59,905,123],{"class":95},[14,907,140],{},[50,909,911],{"className":52,"code":910,"language":54,"meta":55,"style":55},"Hello world\n",[18,912,913],{"__ignoreMap":55},[59,914,915],{"class":61,"line":62},[59,916,910],{"class":65},[14,918,919],{},"The result was created, but it was not stored anywhere.",[14,921,922],{},"To keep the change, save it to a variable:",[50,924,926],{"className":52,"code":925,"language":54,"meta":55,"style":55},"text = \"Hello world\"\nnew_text = text.replace(\"world\", \"Python\")\n\nprint(new_text)\n",[18,927,928,940,970,974],{"__ignoreMap":55},[59,929,930,932,934,936,938],{"class":61,"line":62},[59,931,66],{"class":65},[59,933,70],{"class":69},[59,935,74],{"class":73},[59,937,78],{"class":77},[59,939,81],{"class":73},[59,941,942,944,946,948,950,952,954,956,958,960,962,964,966,968],{"class":61,"line":84},[59,943,87],{"class":65},[59,945,70],{"class":69},[59,947,92],{"class":65},[59,949,21],{"class":95},[59,951,99],{"class":98},[59,953,102],{"class":95},[59,955,105],{"class":73},[59,957,108],{"class":77},[59,959,105],{"class":73},[59,961,113],{"class":95},[59,963,74],{"class":73},[59,965,118],{"class":77},[59,967,105],{"class":73},[59,969,123],{"class":95},[59,971,972],{"class":61,"line":126},[59,973,287],{"emptyLinePlaceholder":286},[59,975,976,978,980,982],{"class":61,"line":290},[59,977,130],{"class":129},[59,979,102],{"class":95},[59,981,135],{"class":98},[59,983,123],{"class":95},[14,985,140],{},[50,987,988],{"className":52,"code":143,"language":54,"meta":55,"style":55},[18,989,990],{"__ignoreMap":55},[59,991,992],{"class":61,"line":62},[59,993,143],{"class":65},[14,995,996],{},"You can also assign it back to the same variable:",[50,998,1000],{"className":52,"code":999,"language":54,"meta":55,"style":55},"text = \"Hello world\"\ntext = text.replace(\"world\", \"Python\")\n\nprint(text)\n",[18,1001,1002,1014,1044,1048],{"__ignoreMap":55},[59,1003,1004,1006,1008,1010,1012],{"class":61,"line":62},[59,1005,66],{"class":65},[59,1007,70],{"class":69},[59,1009,74],{"class":73},[59,1011,78],{"class":77},[59,1013,81],{"class":73},[59,1015,1016,1018,1020,1022,1024,1026,1028,1030,1032,1034,1036,1038,1040,1042],{"class":61,"line":84},[59,1017,66],{"class":65},[59,1019,70],{"class":69},[59,1021,92],{"class":65},[59,1023,21],{"class":95},[59,1025,99],{"class":98},[59,1027,102],{"class":95},[59,1029,105],{"class":73},[59,1031,108],{"class":77},[59,1033,105],{"class":73},[59,1035,113],{"class":95},[59,1037,74],{"class":73},[59,1039,118],{"class":77},[59,1041,105],{"class":73},[59,1043,123],{"class":95},[59,1045,1046],{"class":61,"line":126},[59,1047,287],{"emptyLinePlaceholder":286},[59,1049,1050,1052,1054,1056],{"class":61,"line":290},[59,1051,130],{"class":129},[59,1053,102],{"class":95},[59,1055,196],{"class":98},[59,1057,123],{"class":95},[14,1059,140],{},[50,1061,1062],{"className":52,"code":143,"language":54,"meta":55,"style":55},[18,1063,1064],{"__ignoreMap":55},[59,1065,1066],{"class":61,"line":62},[59,1067,143],{"class":65},[14,1069,1070,1071,1075,1076,21],{},"This idea is important in many string tasks, including ",[334,1072,1074],{"href":1073},"\u002Fhow-to\u002Fhow-to-split-a-string-in-python\u002F","splitting a string in Python"," and ",[334,1077,1079],{"href":1078},"\u002Fhow-to\u002Fhow-to-join-strings-in-python\u002F","joining strings in Python",[45,1081,1083],{"id":1082},"case-matters-when-replacing-text","Case matters when replacing text",[14,1085,1086,1088],{},[18,1087,347],{}," is case-sensitive.",[14,1090,1091,1092,1075,1095,1098],{},"That means ",[18,1093,1094],{},"\"Cat\"",[18,1096,1097],{},"\"cat\""," are different.",[50,1100,1102],{"className":52,"code":1101,"language":54,"meta":55,"style":55},"text = \"Cat cat CAT\"\nnew_text = text.replace(\"cat\", \"dog\")\n\nprint(new_text)\n",[18,1103,1104,1117,1147,1151],{"__ignoreMap":55},[59,1105,1106,1108,1110,1112,1115],{"class":61,"line":62},[59,1107,66],{"class":65},[59,1109,70],{"class":69},[59,1111,74],{"class":73},[59,1113,1114],{"class":77},"Cat cat CAT",[59,1116,81],{"class":73},[59,1118,1119,1121,1123,1125,1127,1129,1131,1133,1135,1137,1139,1141,1143,1145],{"class":61,"line":84},[59,1120,87],{"class":65},[59,1122,70],{"class":69},[59,1124,92],{"class":65},[59,1126,21],{"class":95},[59,1128,99],{"class":98},[59,1130,102],{"class":95},[59,1132,105],{"class":73},[59,1134,783],{"class":77},[59,1136,105],{"class":73},[59,1138,113],{"class":95},[59,1140,74],{"class":73},[59,1142,792],{"class":77},[59,1144,105],{"class":73},[59,1146,123],{"class":95},[59,1148,1149],{"class":61,"line":126},[59,1150,287],{"emptyLinePlaceholder":286},[59,1152,1153,1155,1157,1159],{"class":61,"line":290},[59,1154,130],{"class":129},[59,1156,102],{"class":95},[59,1158,135],{"class":98},[59,1160,123],{"class":95},[14,1162,140],{},[50,1164,1166],{"className":52,"code":1165,"language":54,"meta":55,"style":55},"Cat dog CAT\n",[18,1167,1168],{"__ignoreMap":55},[59,1169,1170,1173],{"class":61,"line":62},[59,1171,1172],{"class":65},"Cat dog ",[59,1174,1175],{"class":740},"CAT\n",[14,1177,1178,1179,1181],{},"Only the lowercase ",[18,1180,1097],{}," was replaced.",[14,1183,1184],{},"If nothing gets replaced, check:",[26,1186,1187,1190,1193,1196],{},[29,1188,1189],{},"uppercase and lowercase letters",[29,1191,1192],{},"spelling",[29,1194,1195],{},"spaces before or after the text",[29,1197,1198],{},"punctuation",[14,1200,1201,1202,1205],{},"Using ",[18,1203,1204],{},"repr()"," can help you see hidden spaces:",[50,1207,1209],{"className":52,"code":1208,"language":54,"meta":55,"style":55},"text = \"hello \"\nprint(repr(text))\n",[18,1210,1211,1224],{"__ignoreMap":55},[59,1212,1213,1215,1217,1219,1222],{"class":61,"line":62},[59,1214,66],{"class":65},[59,1216,70],{"class":69},[59,1218,74],{"class":73},[59,1220,1221],{"class":77},"hello ",[59,1223,81],{"class":73},[59,1225,1226,1228,1230,1233,1235,1237],{"class":61,"line":84},[59,1227,130],{"class":129},[59,1229,102],{"class":95},[59,1231,1232],{"class":129},"repr",[59,1234,102],{"class":95},[59,1236,196],{"class":98},[59,1238,1239],{"class":95},"))\n",[14,1241,140],{},[50,1243,1245],{"className":52,"code":1244,"language":54,"meta":55,"style":55},"'hello '\n",[18,1246,1247],{"__ignoreMap":55},[59,1248,1249,1253,1256],{"class":61,"line":62},[59,1250,1252],{"class":1251},"s2W-s","'",[59,1254,1221],{"class":1255},"sithA",[59,1257,1258],{"class":1251},"'\n",[14,1260,1261],{},"That trailing space may stop a match from working.",[14,1263,1264,1265,21],{},"If your goal is cleanup, you may also want to learn ",[334,1266,1268],{"href":1267},"\u002Fhow-to\u002Fhow-to-remove-whitespace-from-a-string-in-python\u002F","how to remove whitespace from a string in Python",[45,1270,1272,1273,1275],{"id":1271},"when-replace-is-enough-and-when-it-is-not","When ",[18,1274,347],{}," is enough and when it is not",[14,1277,152,1278,1280],{},[18,1279,347],{}," when you want to replace an exact piece of text.",[14,1282,1283],{},"Good examples:",[26,1285,1286,1295,1305,1308],{},[29,1287,1288,1289,1291,1292],{},"replacing ",[18,1290,1097],{}," with ",[18,1293,1294],{},"\"dog\"",[29,1296,1297,1298,1301,1302],{},"changing ",[18,1299,1300],{},"\"\u002F\""," to ",[18,1303,1304],{},"\"-\"",[29,1306,1307],{},"removing a known word or symbol",[29,1309,1310],{},"replacing a short fixed phrase",[14,1312,659],{},[50,1314,1316],{"className":52,"code":1315,"language":54,"meta":55,"style":55},"text = \"Price: $10\"\nnew_text = text.replace(\"$\", \"\")\n\nprint(new_text)\n",[18,1317,1318,1331,1359,1363],{"__ignoreMap":55},[59,1319,1320,1322,1324,1326,1329],{"class":61,"line":62},[59,1321,66],{"class":65},[59,1323,70],{"class":69},[59,1325,74],{"class":73},[59,1327,1328],{"class":77},"Price: $10",[59,1330,81],{"class":73},[59,1332,1333,1335,1337,1339,1341,1343,1345,1347,1350,1352,1354,1357],{"class":61,"line":84},[59,1334,87],{"class":65},[59,1336,70],{"class":69},[59,1338,92],{"class":65},[59,1340,21],{"class":95},[59,1342,99],{"class":98},[59,1344,102],{"class":95},[59,1346,105],{"class":73},[59,1348,1349],{"class":77},"$",[59,1351,105],{"class":73},[59,1353,113],{"class":95},[59,1355,1356],{"class":73}," \"\"",[59,1358,123],{"class":95},[59,1360,1361],{"class":61,"line":126},[59,1362,287],{"emptyLinePlaceholder":286},[59,1364,1365,1367,1369,1371],{"class":61,"line":290},[59,1366,130],{"class":129},[59,1368,102],{"class":95},[59,1370,135],{"class":98},[59,1372,123],{"class":95},[14,1374,140],{},[50,1376,1378],{"className":52,"code":1377,"language":54,"meta":55,"style":55},"Price: 10\n",[18,1379,1380],{"__ignoreMap":55},[59,1381,1382,1385,1388],{"class":61,"line":62},[59,1383,1384],{"class":65},"Price",[59,1386,1387],{"class":95},":",[59,1389,1390],{"class":595}," 10\n",[14,1392,1393,1395],{},[18,1394,347],{}," is enough for many everyday string tasks.",[14,1397,1398],{},"But it only matches exact text. If the text is different from what you search for, nothing will change.",[14,1400,1401],{},"For example:",[50,1403,1405],{"className":52,"code":1404,"language":54,"meta":55,"style":55},"text = \"Hello, world!\"\nnew_text = text.replace(\"world\", \"Python\")\n\nprint(new_text)\n",[18,1406,1407,1420,1450,1454],{"__ignoreMap":55},[59,1408,1409,1411,1413,1415,1418],{"class":61,"line":62},[59,1410,66],{"class":65},[59,1412,70],{"class":69},[59,1414,74],{"class":73},[59,1416,1417],{"class":77},"Hello, world!",[59,1419,81],{"class":73},[59,1421,1422,1424,1426,1428,1430,1432,1434,1436,1438,1440,1442,1444,1446,1448],{"class":61,"line":84},[59,1423,87],{"class":65},[59,1425,70],{"class":69},[59,1427,92],{"class":65},[59,1429,21],{"class":95},[59,1431,99],{"class":98},[59,1433,102],{"class":95},[59,1435,105],{"class":73},[59,1437,108],{"class":77},[59,1439,105],{"class":73},[59,1441,113],{"class":95},[59,1443,74],{"class":73},[59,1445,118],{"class":77},[59,1447,105],{"class":73},[59,1449,123],{"class":95},[59,1451,1452],{"class":61,"line":126},[59,1453,287],{"emptyLinePlaceholder":286},[59,1455,1456,1458,1460,1462],{"class":61,"line":290},[59,1457,130],{"class":129},[59,1459,102],{"class":95},[59,1461,135],{"class":98},[59,1463,123],{"class":95},[14,1465,140],{},[50,1467,1469],{"className":52,"code":1468,"language":54,"meta":55,"style":55},"Hello, Python!\n",[18,1470,1471],{"__ignoreMap":55},[59,1472,1473,1476,1478],{"class":61,"line":62},[59,1474,1475],{"class":65},"Hello",[59,1477,113],{"class":95},[59,1479,1480],{"class":65}," Python!\n",[14,1482,1483,1484,1487],{},"This works because ",[18,1485,1486],{},"\"world\""," is an exact match inside the string.",[14,1489,1490,1491,1498,1499,21],{},"If you want full method details, see the ",[334,1492,1494,1495,1497],{"href":1493},"\u002Freference\u002Fpython-string-replace-method","Python string ",[18,1496,347],{}," method reference",". If you need to check whether text exists before replacing it, see ",[334,1500,1502],{"href":1501},"\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",[45,1504,1506],{"id":1505},"common-mistakes","Common mistakes",[14,1508,1509,1510,1512],{},"Here are the most common reasons ",[18,1511,347],{}," does not work as expected.",[1514,1515,1517],"h3",{"id":1516},"forgetting-to-store-the-returned-string","Forgetting to store the returned string",[50,1519,1521],{"className":52,"code":1520,"language":54,"meta":55,"style":55},"text = \"apple pie\"\ntext.replace(\"apple\", \"cherry\")\n\nprint(text)\n",[18,1522,1523,1536,1564,1568],{"__ignoreMap":55},[59,1524,1525,1527,1529,1531,1534],{"class":61,"line":62},[59,1526,66],{"class":65},[59,1528,70],{"class":69},[59,1530,74],{"class":73},[59,1532,1533],{"class":77},"apple pie",[59,1535,81],{"class":73},[59,1537,1538,1540,1542,1544,1546,1548,1551,1553,1555,1557,1560,1562],{"class":61,"line":84},[59,1539,196],{"class":65},[59,1541,21],{"class":95},[59,1543,99],{"class":98},[59,1545,102],{"class":95},[59,1547,105],{"class":73},[59,1549,1550],{"class":77},"apple",[59,1552,105],{"class":73},[59,1554,113],{"class":95},[59,1556,74],{"class":73},[59,1558,1559],{"class":77},"cherry",[59,1561,105],{"class":73},[59,1563,123],{"class":95},[59,1565,1566],{"class":61,"line":126},[59,1567,287],{"emptyLinePlaceholder":286},[59,1569,1570,1572,1574,1576],{"class":61,"line":290},[59,1571,130],{"class":129},[59,1573,102],{"class":95},[59,1575,196],{"class":98},[59,1577,123],{"class":95},[14,1579,140],{},[50,1581,1583],{"className":52,"code":1582,"language":54,"meta":55,"style":55},"apple pie\n",[18,1584,1585],{"__ignoreMap":55},[59,1586,1587],{"class":61,"line":62},[59,1588,1582],{"class":65},[14,1590,1591],{},"Fix it by saving the result:",[50,1593,1595],{"className":52,"code":1594,"language":54,"meta":55,"style":55},"text = \"apple pie\"\ntext = text.replace(\"apple\", \"cherry\")\n\nprint(text)\n",[18,1596,1597,1609,1639,1643],{"__ignoreMap":55},[59,1598,1599,1601,1603,1605,1607],{"class":61,"line":62},[59,1600,66],{"class":65},[59,1602,70],{"class":69},[59,1604,74],{"class":73},[59,1606,1533],{"class":77},[59,1608,81],{"class":73},[59,1610,1611,1613,1615,1617,1619,1621,1623,1625,1627,1629,1631,1633,1635,1637],{"class":61,"line":84},[59,1612,66],{"class":65},[59,1614,70],{"class":69},[59,1616,92],{"class":65},[59,1618,21],{"class":95},[59,1620,99],{"class":98},[59,1622,102],{"class":95},[59,1624,105],{"class":73},[59,1626,1550],{"class":77},[59,1628,105],{"class":73},[59,1630,113],{"class":95},[59,1632,74],{"class":73},[59,1634,1559],{"class":77},[59,1636,105],{"class":73},[59,1638,123],{"class":95},[59,1640,1641],{"class":61,"line":126},[59,1642,287],{"emptyLinePlaceholder":286},[59,1644,1645,1647,1649,1651],{"class":61,"line":290},[59,1646,130],{"class":129},[59,1648,102],{"class":95},[59,1650,196],{"class":98},[59,1652,123],{"class":95},[1514,1654,1656,1657,1659],{"id":1655},"expecting-replace-to-change-the-original-string-directly","Expecting ",[18,1658,347],{}," to change the original string directly",[14,1661,1662],{},"Strings cannot be changed in place.",[14,1664,1665],{},"Always remember:",[26,1667,1668,1673],{},[29,1669,1670,1672],{},[18,1671,347],{}," returns a new string",[29,1674,1675],{},"it does not edit the existing string object",[1514,1677,1679],{"id":1678},"using-the-wrong-letter-case","Using the wrong letter case",[50,1681,1683],{"className":52,"code":1682,"language":54,"meta":55,"style":55},"text = \"Python\"\nprint(text.replace(\"python\", \"Java\"))\n",[18,1684,1685,1697],{"__ignoreMap":55},[59,1686,1687,1689,1691,1693,1695],{"class":61,"line":62},[59,1688,66],{"class":65},[59,1690,70],{"class":69},[59,1692,74],{"class":73},[59,1694,118],{"class":77},[59,1696,81],{"class":73},[59,1698,1699,1701,1703,1705,1707,1709,1711,1713,1715,1717,1719,1721,1724,1726],{"class":61,"line":84},[59,1700,130],{"class":129},[59,1702,102],{"class":95},[59,1704,196],{"class":98},[59,1706,21],{"class":95},[59,1708,99],{"class":98},[59,1710,102],{"class":95},[59,1712,105],{"class":73},[59,1714,54],{"class":77},[59,1716,105],{"class":73},[59,1718,113],{"class":95},[59,1720,74],{"class":73},[59,1722,1723],{"class":77},"Java",[59,1725,105],{"class":73},[59,1727,1239],{"class":95},[14,1729,140],{},[50,1731,1733],{"className":52,"code":1732,"language":54,"meta":55,"style":55},"Python\n",[18,1734,1735],{"__ignoreMap":55},[59,1736,1737],{"class":61,"line":62},[59,1738,1732],{"class":65},[14,1740,1741,1742,1745,1746,21],{},"Nothing changed because ",[18,1743,1744],{},"\"python\""," does not exactly match ",[18,1747,1748],{},"\"Python\"",[1514,1750,1752],{"id":1751},"misspelling-the-old-text","Misspelling the old text",[50,1754,1756],{"className":52,"code":1755,"language":54,"meta":55,"style":55},"text = \"I love programming\"\nprint(text.replace(\"programing\", \"Python\"))\n",[18,1757,1758,1771],{"__ignoreMap":55},[59,1759,1760,1762,1764,1766,1769],{"class":61,"line":62},[59,1761,66],{"class":65},[59,1763,70],{"class":69},[59,1765,74],{"class":73},[59,1767,1768],{"class":77},"I love programming",[59,1770,81],{"class":73},[59,1772,1773,1775,1777,1779,1781,1783,1785,1787,1790,1792,1794,1796,1798,1800],{"class":61,"line":84},[59,1774,130],{"class":129},[59,1776,102],{"class":95},[59,1778,196],{"class":98},[59,1780,21],{"class":95},[59,1782,99],{"class":98},[59,1784,102],{"class":95},[59,1786,105],{"class":73},[59,1788,1789],{"class":77},"programing",[59,1791,105],{"class":73},[59,1793,113],{"class":95},[59,1795,74],{"class":73},[59,1797,118],{"class":77},[59,1799,105],{"class":73},[59,1801,1239],{"class":95},[14,1803,140],{},[50,1805,1807],{"className":52,"code":1806,"language":54,"meta":55,"style":55},"I love programming\n",[18,1808,1809],{"__ignoreMap":55},[59,1810,1811],{"class":61,"line":62},[59,1812,1806],{"class":65},[14,1814,1815],{},"The old text must match exactly.",[1514,1817,1819],{"id":1818},"expecting-only-the-first-match-to-change","Expecting only the first match to change",[14,1821,1822],{},"By default, all matches are replaced:",[50,1824,1826],{"className":52,"code":1825,"language":54,"meta":55,"style":55},"text = \"ha ha ha\"\nprint(text.replace(\"ha\", \"ho\"))\n",[18,1827,1828,1841],{"__ignoreMap":55},[59,1829,1830,1832,1834,1836,1839],{"class":61,"line":62},[59,1831,66],{"class":65},[59,1833,70],{"class":69},[59,1835,74],{"class":73},[59,1837,1838],{"class":77},"ha ha ha",[59,1840,81],{"class":73},[59,1842,1843,1845,1847,1849,1851,1853,1855,1857,1860,1862,1864,1866,1869,1871],{"class":61,"line":84},[59,1844,130],{"class":129},[59,1846,102],{"class":95},[59,1848,196],{"class":98},[59,1850,21],{"class":95},[59,1852,99],{"class":98},[59,1854,102],{"class":95},[59,1856,105],{"class":73},[59,1858,1859],{"class":77},"ha",[59,1861,105],{"class":73},[59,1863,113],{"class":95},[59,1865,74],{"class":73},[59,1867,1868],{"class":77},"ho",[59,1870,105],{"class":73},[59,1872,1239],{"class":95},[14,1874,140],{},[50,1876,1878],{"className":52,"code":1877,"language":54,"meta":55,"style":55},"ho ho ho\n",[18,1879,1880],{"__ignoreMap":55},[59,1881,1882],{"class":61,"line":62},[59,1883,1877],{"class":65},[14,1885,1886,1887,1387],{},"If you only want the first match, use ",[18,1888,652],{},[50,1890,1892],{"className":52,"code":1891,"language":54,"meta":55,"style":55},"text = \"ha ha ha\"\nprint(text.replace(\"ha\", \"ho\", 1))\n",[18,1893,1894,1906],{"__ignoreMap":55},[59,1895,1896,1898,1900,1902,1904],{"class":61,"line":62},[59,1897,66],{"class":65},[59,1899,70],{"class":69},[59,1901,74],{"class":73},[59,1903,1838],{"class":77},[59,1905,81],{"class":73},[59,1907,1908,1910,1912,1914,1916,1918,1920,1922,1924,1926,1928,1930,1932,1934,1936,1938],{"class":61,"line":84},[59,1909,130],{"class":129},[59,1911,102],{"class":95},[59,1913,196],{"class":98},[59,1915,21],{"class":95},[59,1917,99],{"class":98},[59,1919,102],{"class":95},[59,1921,105],{"class":73},[59,1923,1859],{"class":77},[59,1925,105],{"class":73},[59,1927,113],{"class":95},[59,1929,74],{"class":73},[59,1931,1868],{"class":77},[59,1933,105],{"class":73},[59,1935,113],{"class":95},[59,1937,712],{"class":595},[59,1939,1239],{"class":95},[14,1941,140],{},[50,1943,1945],{"className":52,"code":1944,"language":54,"meta":55,"style":55},"ho ha ha\n",[18,1946,1947],{"__ignoreMap":55},[59,1948,1949],{"class":61,"line":62},[59,1950,1944],{"class":65},[45,1952,1954],{"id":1953},"helpful-debugging-checks","Helpful debugging checks",[14,1956,1957],{},"If your replacement is not working, these quick checks can help:",[50,1959,1961],{"className":52,"code":1960,"language":54,"meta":55,"style":55},"print(text)\nprint(repr(text))\nprint(text.replace('old', 'new'))\nprint(text.count('old'))\nprint(type(text))\n",[18,1962,1963,1973,1987,2018,2040],{"__ignoreMap":55},[59,1964,1965,1967,1969,1971],{"class":61,"line":62},[59,1966,130],{"class":129},[59,1968,102],{"class":95},[59,1970,196],{"class":98},[59,1972,123],{"class":95},[59,1974,1975,1977,1979,1981,1983,1985],{"class":61,"line":84},[59,1976,130],{"class":129},[59,1978,102],{"class":95},[59,1980,1232],{"class":129},[59,1982,102],{"class":95},[59,1984,196],{"class":98},[59,1986,1239],{"class":95},[59,1988,1989,1991,1993,1995,1997,1999,2001,2003,2005,2007,2009,2012,2014,2016],{"class":61,"line":126},[59,1990,130],{"class":129},[59,1992,102],{"class":95},[59,1994,196],{"class":98},[59,1996,21],{"class":95},[59,1998,99],{"class":98},[59,2000,102],{"class":95},[59,2002,1252],{"class":73},[59,2004,205],{"class":77},[59,2006,1252],{"class":73},[59,2008,113],{"class":95},[59,2010,2011],{"class":73}," '",[59,2013,224],{"class":77},[59,2015,1252],{"class":73},[59,2017,1239],{"class":95},[59,2019,2020,2022,2024,2026,2028,2030,2032,2034,2036,2038],{"class":61,"line":290},[59,2021,130],{"class":129},[59,2023,102],{"class":95},[59,2025,196],{"class":98},[59,2027,21],{"class":95},[59,2029,652],{"class":98},[59,2031,102],{"class":95},[59,2033,1252],{"class":73},[59,2035,205],{"class":77},[59,2037,1252],{"class":73},[59,2039,1239],{"class":95},[59,2041,2043,2045,2047,2051,2053,2055],{"class":61,"line":2042},5,[59,2044,130],{"class":129},[59,2046,102],{"class":95},[59,2048,2050],{"class":2049},"sZMiF","type",[59,2052,102],{"class":95},[59,2054,196],{"class":98},[59,2056,1239],{"class":95},[14,2058,2059],{},"What these help you check:",[26,2061,2062,2068,2074,2080,2086],{},[29,2063,2064,2067],{},[18,2065,2066],{},"print(text)"," shows the current value",[29,2069,2070,2073],{},[18,2071,2072],{},"print(repr(text))"," shows hidden spaces or escape characters",[29,2075,2076,2079],{},[18,2077,2078],{},"print(text.replace('old', 'new'))"," lets you test the replacement directly",[29,2081,2082,2085],{},[18,2083,2084],{},"print(text.count('old'))"," shows how many matches exist",[29,2087,2088,2091,2092,2094],{},[18,2089,2090],{},"print(type(text))"," confirms that ",[18,2093,196],{}," is actually a string",[14,2096,659],{},[50,2098,2100],{"className":52,"code":2099,"language":54,"meta":55,"style":55},"text = \"cat cat dog\"\n\nprint(text)\nprint(repr(text))\nprint(text.replace(\"cat\", \"bird\"))\nprint(text.count(\"cat\"))\nprint(type(text))\n",[18,2101,2102,2115,2119,2129,2143,2174,2197],{"__ignoreMap":55},[59,2103,2104,2106,2108,2110,2113],{"class":61,"line":62},[59,2105,66],{"class":65},[59,2107,70],{"class":69},[59,2109,74],{"class":73},[59,2111,2112],{"class":77},"cat cat dog",[59,2114,81],{"class":73},[59,2116,2117],{"class":61,"line":84},[59,2118,287],{"emptyLinePlaceholder":286},[59,2120,2121,2123,2125,2127],{"class":61,"line":126},[59,2122,130],{"class":129},[59,2124,102],{"class":95},[59,2126,196],{"class":98},[59,2128,123],{"class":95},[59,2130,2131,2133,2135,2137,2139,2141],{"class":61,"line":290},[59,2132,130],{"class":129},[59,2134,102],{"class":95},[59,2136,1232],{"class":129},[59,2138,102],{"class":95},[59,2140,196],{"class":98},[59,2142,1239],{"class":95},[59,2144,2145,2147,2149,2151,2153,2155,2157,2159,2161,2163,2165,2167,2170,2172],{"class":61,"line":2042},[59,2146,130],{"class":129},[59,2148,102],{"class":95},[59,2150,196],{"class":98},[59,2152,21],{"class":95},[59,2154,99],{"class":98},[59,2156,102],{"class":95},[59,2158,105],{"class":73},[59,2160,783],{"class":77},[59,2162,105],{"class":73},[59,2164,113],{"class":95},[59,2166,74],{"class":73},[59,2168,2169],{"class":77},"bird",[59,2171,105],{"class":73},[59,2173,1239],{"class":95},[59,2175,2177,2179,2181,2183,2185,2187,2189,2191,2193,2195],{"class":61,"line":2176},6,[59,2178,130],{"class":129},[59,2180,102],{"class":95},[59,2182,196],{"class":98},[59,2184,21],{"class":95},[59,2186,652],{"class":98},[59,2188,102],{"class":95},[59,2190,105],{"class":73},[59,2192,783],{"class":77},[59,2194,105],{"class":73},[59,2196,1239],{"class":95},[59,2198,2200,2202,2204,2206,2208,2210],{"class":61,"line":2199},7,[59,2201,130],{"class":129},[59,2203,102],{"class":95},[59,2205,2050],{"class":2049},[59,2207,102],{"class":95},[59,2209,196],{"class":98},[59,2211,1239],{"class":95},[14,2213,140],{},[50,2215,2217],{"className":52,"code":2216,"language":54,"meta":55,"style":55},"cat cat dog\n'cat cat dog'\nbird bird dog\n2\n\u003Cclass 'str'>\n",[18,2218,2219,2224,2232,2237,2242],{"__ignoreMap":55},[59,2220,2221],{"class":61,"line":62},[59,2222,2223],{"class":65},"cat cat dog\n",[59,2225,2226,2228,2230],{"class":61,"line":84},[59,2227,1252],{"class":1251},[59,2229,2112],{"class":1255},[59,2231,1258],{"class":1251},[59,2233,2234],{"class":61,"line":126},[59,2235,2236],{"class":65},"bird bird dog\n",[59,2238,2239],{"class":61,"line":290},[59,2240,2241],{"class":595},"2\n",[59,2243,2244,2247,2251,2253,2256,2258],{"class":61,"line":2042},[59,2245,2246],{"class":69},"\u003C",[59,2248,2250],{"class":2249},"sbsja","class",[59,2252,2011],{"class":73},[59,2254,2255],{"class":77},"str",[59,2257,1252],{"class":73},[59,2259,2260],{"class":69},">\n",[45,2262,2264],{"id":2263},"faq","FAQ",[1514,2266,2268,2269,2271],{"id":2267},"does-replace-change-the-original-string","Does ",[18,2270,347],{}," change the original string?",[14,2273,2274],{},"No. It returns a new string. You must save the result.",[1514,2276,2278],{"id":2277},"how-do-i-replace-only-the-first-match","How do I replace only the first match?",[14,2280,2281],{},"Use the third argument:",[50,2283,2285],{"className":52,"code":2284,"language":54,"meta":55,"style":55},"text.replace(old, new, 1)\n",[18,2286,2287],{"__ignoreMap":55},[59,2288,2289,2291,2293,2295,2297,2299,2301,2303,2305,2307],{"class":61,"line":62},[59,2290,196],{"class":65},[59,2292,21],{"class":95},[59,2294,99],{"class":98},[59,2296,102],{"class":95},[59,2298,205],{"class":98},[59,2300,113],{"class":95},[59,2302,210],{"class":98},[59,2304,113],{"class":95},[59,2306,712],{"class":595},[59,2308,123],{"class":95},[1514,2310,2312],{"id":2311},"why-did-nothing-get-replaced","Why did nothing get replaced?",[14,2314,2315],{},"The exact text may not match. Check:",[26,2317,2318,2320,2322,2324],{},[29,2319,1192],{},[29,2321,437],{},[29,2323,1198],{},[29,2325,1189],{},[1514,2327,2329],{"id":2328},"can-i-replace-multiple-different-words-at-once","Can I replace multiple different words at once?",[14,2331,2332,2333,2335],{},"Not in one basic ",[18,2334,347],{}," call.",[14,2337,2338],{},"For simple cases, use multiple calls:",[50,2340,2342],{"className":52,"code":2341,"language":54,"meta":55,"style":55},"text = \"red blue green\"\ntext = text.replace(\"red\", \"yellow\")\ntext = text.replace(\"blue\", \"purple\")\n\nprint(text)\n",[18,2343,2344,2357,2387,2419,2423],{"__ignoreMap":55},[59,2345,2346,2348,2350,2352,2355],{"class":61,"line":62},[59,2347,66],{"class":65},[59,2349,70],{"class":69},[59,2351,74],{"class":73},[59,2353,2354],{"class":77},"red blue green",[59,2356,81],{"class":73},[59,2358,2359,2361,2363,2365,2367,2369,2371,2373,2375,2377,2379,2381,2383,2385],{"class":61,"line":84},[59,2360,66],{"class":65},[59,2362,70],{"class":69},[59,2364,92],{"class":65},[59,2366,21],{"class":95},[59,2368,99],{"class":98},[59,2370,102],{"class":95},[59,2372,105],{"class":73},[59,2374,385],{"class":77},[59,2376,105],{"class":73},[59,2378,113],{"class":95},[59,2380,74],{"class":73},[59,2382,394],{"class":77},[59,2384,105],{"class":73},[59,2386,123],{"class":95},[59,2388,2389,2391,2393,2395,2397,2399,2401,2403,2406,2408,2410,2412,2415,2417],{"class":61,"line":126},[59,2390,66],{"class":65},[59,2392,70],{"class":69},[59,2394,92],{"class":65},[59,2396,21],{"class":95},[59,2398,99],{"class":98},[59,2400,102],{"class":95},[59,2402,105],{"class":73},[59,2404,2405],{"class":77},"blue",[59,2407,105],{"class":73},[59,2409,113],{"class":95},[59,2411,74],{"class":73},[59,2413,2414],{"class":77},"purple",[59,2416,105],{"class":73},[59,2418,123],{"class":95},[59,2420,2421],{"class":61,"line":290},[59,2422,287],{"emptyLinePlaceholder":286},[59,2424,2425,2427,2429,2431],{"class":61,"line":2042},[59,2426,130],{"class":129},[59,2428,102],{"class":95},[59,2430,196],{"class":98},[59,2432,123],{"class":95},[14,2434,140],{},[50,2436,2438],{"className":52,"code":2437,"language":54,"meta":55,"style":55},"yellow purple green\n",[18,2439,2440],{"__ignoreMap":55},[59,2441,2442],{"class":61,"line":62},[59,2443,2437],{"class":65},[45,2445,2447],{"id":2446},"see-also","See also",[26,2449,2450,2456,2460,2465,2470,2475],{},[29,2451,2452],{},[334,2453,1494,2454,1497],{"href":1493},[18,2455,347],{},[29,2457,2458],{},[334,2459,337],{"href":336},[29,2461,2462],{},[334,2463,2464],{"href":1073},"How to split a string in Python",[29,2466,2467],{},[334,2468,2469],{"href":1078},"How to join strings in Python",[29,2471,2472],{},[334,2473,2474],{"href":1267},"How to remove whitespace from a string in Python",[29,2476,2477],{},[334,2478,2479],{"href":1501},"How to check if a string contains a substring in Python",[2481,2482,2483],"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 .sx12J, html code.shiki .sx12J{--shiki-light:#F76D47;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":55,"searchDepth":84,"depth":84,"links":2485},[2486,2487,2488,2490,2491,2492,2493,2494,2496,2504,2505,2512],{"id":47,"depth":84,"text":48},{"id":159,"depth":84,"text":160},{"id":180,"depth":84,"text":2489},"Use str.replace() for most text replacement",{"id":340,"depth":84,"text":341},{"id":613,"depth":84,"text":614},{"id":832,"depth":84,"text":833},{"id":1082,"depth":84,"text":1083},{"id":1271,"depth":84,"text":2495},"When replace() is enough and when it is not",{"id":1505,"depth":84,"text":1506,"children":2497},[2498,2499,2501,2502,2503],{"id":1516,"depth":126,"text":1517},{"id":1655,"depth":126,"text":2500},"Expecting replace() to change the original string directly",{"id":1678,"depth":126,"text":1679},{"id":1751,"depth":126,"text":1752},{"id":1818,"depth":126,"text":1819},{"id":1953,"depth":84,"text":1954},{"id":2263,"depth":84,"text":2264,"children":2506},[2507,2509,2510,2511],{"id":2267,"depth":126,"text":2508},"Does replace() change the original string?",{"id":2277,"depth":126,"text":2278},{"id":2311,"depth":126,"text":2312},{"id":2328,"depth":126,"text":2329},{"id":2446,"depth":84,"text":2447},"Master how to replace text in a string in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-replace-text-in-a-string-in-python",{"title":5,"description":2513},"how-to\u002Fhow-to-replace-text-in-a-string-in-python","OF7C0Zcr99oqW9RXozvE2G0PPTU_Z2_1RH4qaMBh76o",1777585506301]