Python SyntaxError: inconsistent use of tabs and spaces in indentation














































Python SyntaxError: inconsistent use of tabs and spaces in indentation



Python SyntaxError: inconsistent use of tabs and spaces in indentation


In python, indentation is required to indicate what block of code a statement belongs to. The statements within the block should be indented at same level. Indentation makes program easier to understand and read.

Python won't allow combining the tabs and spaces for indentation which results in syntax error.

How to resolve?

Have a look at your code and make sure that you either use tabs or spaces for indentation but not both.

For example:

#To print subset of string

string='Cppsecrets'
n=len(string)
arr=[]
for i in range(n):
for j in range(i+1,n+1):
a=string[i:j]
arr.append(a)
print(arr)


The problem in above code is that, both tabs and spaces are used for indentation in for loop. Look at line 8 and line 9.

Error:

inconsistent use of tabs and spaces in indentation

When you indent one with tab and other with spaces, python throws error though visually appears as same in some text editors.

Solution:


1. In Text Editors:

One way to get rid of error is go to settings tab in your text editor and enable 'Convert tabs to spaces' feature which automatically replaces tab with spaces characters.

2. In Python IDLE:

Go to format and click on 'untabify region' and edit tab width.

Comments