{"id":2431,"date":"2025-10-10T10:28:11","date_gmt":"2025-10-10T10:28:11","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2431"},"modified":"2026-02-05T11:58:56","modified_gmt":"2026-02-05T11:58:56","slug":"fix-indentationerror-unexpected-indent-python","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/fix-indentationerror-unexpected-indent-python\/","title":{"rendered":"How to fix IndentationError: unexpected indent in Python?"},"content":{"rendered":"\n<p>Python doesn\u2019t mess around when it comes to indentation. If your code has even one line that\u2019s indented more than expected, Python will throw an IndentationError. Let\u2019s look at why this happens, how to fix it, and how to avoid it in future projects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What the Error Looks Like<\/h2>\n\n\n\n<p>Here\u2019s a typical example:<\/p>\n\n\n\n<p><strong>This error shows up when you run your Python script and see something like:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def say_hello():\n    print(\"Hello\")\n        print(\"World\")  # &lt;- Unexpected indent<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why This Error Happens<\/h2>\n\n\n\n<p>Python uses indentation to define code blocks, unlike many other languages that use curly braces ({}).<\/p>\n\n\n\n<p>Python shows this error when there\u2019s an extra space or tab where it shouldn\u2019t be.<\/p>\n\n\n\n<p><strong>Common reasons:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A line starts with a space or tab by mistake<\/li>\n\n\n\n<li>Adding tabs and spaces in the same file<br><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Fix the Error<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Check for Inconsistent Indentation<\/h3>\n\n\n\n<p>All statements within a block (like inside a function or if statement) should be indented at the same level.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet():\n    print(\"Hi\")       # Correct\n     print(\"there\")   # \u274c Incorrect -- unexpected indent<\/code><\/pre>\n\n\n\n<p><strong>Fix:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet():\n    print(\"Hi\")\n    print(\"there\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Avoid Mixing Tabs and Spaces<\/h3>\n\n\n\n<p>Some editors insert spaces; others insert tabs. Mixing both in the same file causes indentation errors.<\/p>\n\n\n\n<p><strong>Use this to convert tabs to spaces (recommended):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># In VSCode, enable:\n# \"Insert Spaces\" instead of \"Keep Tabs\"<\/code><\/pre>\n\n\n\n<p><strong>Or use this Python tool:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>autopep8 yourfile.py --in-place<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use a Linter or Code Formatter<\/h3>\n\n\n\n<p><strong>Install tools like:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flake8<\/li>\n\n\n\n<li>black<\/li>\n\n\n\n<li>autopep8<\/li>\n<\/ul>\n\n\n\n<p>These can detect and fix indentation issues automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>IndentationError: unexpected indent means your code\u2019s structure is off and Python can\u2019t interpret it. The best way to avoid this is to stick with 4-space indentation, avoid mixing tabs and spaces, and use an editor that highlights formatting issues. If your project has growing complexity or collaborative code, it\u2019s worth it to <a href=\"https:\/\/www.cmarix.com\/hire-python-developers.html\">hire Python developers<\/a> who follow clean formatting practices and prevent errors like this before they happen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python doesn\u2019t mess around when it comes to indentation. If your code has even one line that\u2019s indented more than expected, Python will throw an IndentationError. Let\u2019s look at why this happens, how to fix it, and how to avoid it in future projects. What the Error Looks Like Here\u2019s a typical example: This error [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2433,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[163,3],"tags":[],"class_list":["post-2431","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\/2431","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=2431"}],"version-history":[{"count":3,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2431\/revisions"}],"predecessor-version":[{"id":2436,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2431\/revisions\/2436"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2433"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}