TabError: inconsistent use of tabs and spaces in indentation

The TabError: inconsistent use of tabs and spaces in indentation occurs if you indent the code using a combination of whitespaces and tabs in the same code block.

In this guide, we will discuss what is TabError: inconsistent use of tabs and spaces in indentation and how to resolve this error with examples.

TabError: inconsistent use of tabs and spaces in indentation

In Python, indentation is most important as it does not use curly braces syntax like other languages to denote where the code block starts and ends.

Without indentation Python will not know which code to execute next or which statement belongs to which block and will lead to IndentationError.

We can indent the Python code either using spaces or tabs. The Python style guide recommends using spaces for indentation. Further, it states Python disallows mixing tabs and spaces for indentation, and doing so will raise inconsistent use of tabs and spaces in indentation error.

Let us look at an example to demonstrate the issue.

In the above example, we have a method convert_meter_to_cm, and the first line of code is indented with a tab, and the second line is indented with four white spaces.

def convert_meter_to_cm(num):
    output = num * 1000
   return output

convert_meter_to_cm(10)

Output

 File "c:\Personal\IJS\Code\prgm.py", line 3
    return output
                 ^
TabError: inconsistent use of tabs and spaces in indentation

When we execute the program, it clearly shows what the error is and where it occurred with a line number.

How to resolve TabError: inconsistent use of tabs and spaces in indentation?

We have used both spaces and tabs in the same code block, and hence we have got the error in the first place. We can resolve the error by using either space or tab in the code block.

Let us indent the code according to the PEP-8 recommendation in our example, i.e., using four white spaces everywhere.

def convert_meter_to_cm(num):
    output = num * 1000
    return output

print(convert_meter_to_cm(10))

Output

10000

If you are using the VS Code, the easy way to solve this error is by using the settings “Convert indentation to spaces” or “Convert indentation to tabs” commands.

  1. Press CTRL + Shift + P or (⌘ + Shift + P on Mac) to open the command palette.
  2. type “convert indentation to” in the search command palette
  3. select your preferred options, either tab or space
Vs Code Convert Indentation To Spaces Or Tabs
TabError: inconsistent use of tabs and spaces in indentation 2

Python and PEP 8 Guidelines 

  1. Generally, in Python, you follow the four-spaces rule according to PEP 8 standards
  2. Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs.
  3. Do not mix tabs and spaces. Python disallows the mixing of indentation.
  4. Avoid trailing whitespaces anywhere because it’s usually invisible and it causes confusion.

Conclusion

If you mix both tab and space for indentation in the same code block Python will throw TabError: inconsistent use of tabs and spaces in indentation. Python is very strict on indentation and we can use either white spaces or tabs in the same code block to resolve the issue.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Bool()

Python bool()

Table of Contents Hide bool() Syntax bool() Parametersbool() Return ValueExample of bool() function in Python Python’s bool() function converts a given value into Boolean(True or False) using the standard truth testing…
View Post
Indentationerror Unexpected Indent

IndentationError: unexpected indent

Table of Contents Hide What are the reasons for IndentationError: unexpected indent?Python and PEP 8 Guidelines Solving IndentationError: expected an indented blockExample 1 – Indenting inside a functionExample 2 – Indentation…
View Post