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