{"id":2449,"date":"2025-10-24T09:13:37","date_gmt":"2025-10-24T09:13:37","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2449"},"modified":"2026-02-05T11:58:53","modified_gmt":"2026-02-05T11:58:53","slug":"fix-valueerror-invalid-literal-int-python","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/fix-valueerror-invalid-literal-int-python\/","title":{"rendered":"How to fix ValueError: invalid literal for int() with base 10 in Python?"},"content":{"rendered":"\n<p>You&#8217;re trying to convert a string into an integer using int(), but Python throws a ValueError. That usually means the string doesn&#8217;t look like a proper number. Let&#8217;s break down why this happens and how to fix it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = int(\"twenty\")<\/code><\/pre>\n\n\n\n<p>This gives:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ValueError: invalid literal for int() with base 10: 'twenty'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why This Happens<\/h2>\n\n\n\n<p>The int() function expects a string that represents a valid base-10 number, like &#8220;42&#8221; or &#8220;0&#8221;. If you pass it anything else like alphabetic characters, symbols, or a decimal &#8211; it will raise this error.<\/p>\n\n\n\n<p><strong>Common problematic inputs:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&#8220;abc&#8221; \u2192 letters<\/li>\n\n\n\n<li>&#8220;12.5&#8221; \u2192 decimal value (float)<\/li>\n\n\n\n<li>&#8220;&#8221; \u2192 empty string<\/li>\n\n\n\n<li>&#8221; 7 &#8221; \u2192 extra spaces (though this usually works)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Fix the Error in Python<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Print the Value Before Conversion<\/h3>\n\n\n\n<p>Always check what you&#8217;re trying to convert:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(user_input)\nage = int(user_input)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Use str.isdigit() to Check Validity<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>user_input = \"25\"\nif user_input.isdigit():\n    age = int(user_input)\nelse:\n    print(\"Please enter a valid number.\")<\/code><\/pre>\n\n\n\n<p><strong>Note: <\/strong>isdigit() only works for positive integers without decimals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use try-except Block for Safer Conversion<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>user_input = \"12a\"\ntry:\n    age = int(user_input)\nexcept ValueError:\n    print(\"Invalid input. Please enter a number.\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Strip Whitespace and Validate<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cleaned = user_input.strip()\nif cleaned.isdigit():\n    age = int(cleaned)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Handle Decimal Numbers Properly<\/h3>\n\n\n\n<p>If the input might be a decimal (e.g., &#8220;12.5&#8221;), convert it to float instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number = float(\"12.5\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The <strong>ValueError: invalid literal for int()<\/strong> with base 10 shows up when<strong> int()<\/strong> is given a string that doesn\u2019t cleanly represent a whole number. To fix it, first inspect the input, then validate it using methods like <strong>isdigit()<\/strong> or a try-except block. If decimals are expected, use<strong> float() <\/strong>instead of<strong> int()<\/strong>. Clean inputs, validate early, and handle exceptions\u2014that\u2019s the safest way to avoid this error. If you&#8217;re building user-facing forms, APIs, or data pipelines, it pays to <a href=\"https:\/\/www.cmarix.com\/hire-python-developers.html\">hire a Python developer<\/a> who knows how to handle input validation at every layer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;re trying to convert a string into an integer using int(), but Python throws a ValueError. That usually means the string doesn&#8217;t look like a proper number. Let&#8217;s break down why this happens and how to fix it. This gives: Why This Happens The int() function expects a string that represents a valid base-10 number, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2452,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[163,3],"tags":[],"class_list":["post-2449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=2449"}],"version-history":[{"count":6,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2449\/revisions"}],"predecessor-version":[{"id":2457,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2449\/revisions\/2457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2452"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}