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