30th Jan 2023 6 minutes read What 'inconsistent use of tabs and spaces in indentation' Means in Python and How to Fix It Luke Hande python indentation Learn why indentation in Python is so important and how you can fix any mistakes. Indenting blocks of code is an important part of programming; it serves several purposes. First and foremost, it improves readability. Imagine removing all the formatting from a recipe leaving only the spaces between the list of ingredients and instructions. This would be hard for the cook to follow. In Python, indentation also controls how your code behaves. If you change the indentation, you can change what parts of the code are executed and when. Understanding how to indent code properly is important. This article will address a specific question regarding indentation – namely, what to do about the following Python error message: “inconsistent use of tabs and spaces in indentation”. This article is aimed at beginners with little to no programming experience. If you’re just at the start of your programming journey and you need some learning material, consider taking our Python Basics track, which bundles together three of the most useful courses for beginners. As an extension, the Built-in Algorithms in Python course will teach you how to apply common operations to Python data structures. Indentation 101 We’ll illustrate how indentation in Python works with a simple example. The block of code below has a for loop which prints some numbers to the screen. When finished, it will print one last line with the message ‘done’, since the two print() statements are indented to different levels. For more information on the print() function, see A Complete Guide to the Python print() Function. >>> for index in range(3): ... print(index) ... print('done') This is the result: 0 1 2 done Now for the next test, try indenting the final line to the same level as the print(index) line and run the block of code again. You should notice the message ‘done’ is printed directly after each index, since it’s now inside the for loop. The behavior of the program has changed by changing the indentation of the code block. What’s With Whitespaces? Most other languages use curly braces ({}) to tell the compiler where blocks of code begin and end. Python uses white spaces to indicate code blocks; in this sense, it is the odd one out. However, using white spaces for indentation frees up a symbol for another purpose. By not using curly braces to indicate indentation in Python, this symbol can be used to create a dictionary. This is an advantage, since the number of symbols on the keyboard is less than the number of things you want to use symbols for. Python doesn’t force you to indent with either a tab or spaces. Both will work; however, you do need to be consistent. If you mix tabs and spaces in the same program, you’ll receive the error mentioned in the introduction. We’ll take a closer look at this in the next section. Tabs vs. Spaces Let’s demonstrate why mixing tabs and spaces is a bad idea. To reproduce the error in the introduction, open a basic text editor like Notepad and write the following for loop: >>> for index in range(3): ... print(index) ... print('done') The print(index) line should be indented with a tab, and then the print('done') line should be indented with the equivalent number of spaces; both lines are indented to the same level. Save your file as test.py. From the Command Prompt, run: >>> python test.py The output printed to the Command Prompt will include information on where the error occurred as well as the message: TabError: inconsistent use of tabs and spaces in indentation Since tabs and spaces don’t play nicely together, the question now turns to which is best to use for indenting code. At first sight, a tab would seem to be preferable, since it saves some keystrokes. But in different IDE and code editors, the length of a tab may be different, usually either 4 or 8 spaces. This could be a problem if the program is developed in different editors; after indenting with a tab in one editor, the same tab may be a different length in other editors. A space, on the other hand, only takes up one character and avoids this potential problem. This preference for spaces over tabs is reflected in the recommendation provided in the PEP 8 – Style Guide for Python Code. The whole PEP is worth a read, since getting into good style habits early will be beneficial down the road. But the section on Tabs or Spaces? states explicitly: “Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python disallows mixing tabs and spaces for indentation.” Furthermore, the section on Indentation states that 4 spaces per indentation level should be used. So, there you have it. The preferred way to indent code properly in Python is with 4 spaces per level. How to Debug “inconsistent use of tabs and spaces in indentation” If you ever encounter inconsistent use of tabs and spaces in Python, the solution boils down to replacing tabs with the correct number of spaces. Doing this manually is a bad idea, since it will be time consuming and error prone (especially for large programs). Most text editors or IDEs (Integrated Development Environments) have a ‘find and replace’ feature; in Windows, you can normally open it by pressing CTRL + f or by using the toolbar. If you hit the tab key while in the search field, you’ll just move the focus of the window, so you have to copy a tab character (either ‘\t’ or ‘ ‘) into the find field and then type 4 spaces (‘ ’) into the replace field. Problem solved. If you find yourself working on Linux machine where you don’t have a text editor installed (e.g. if you work on a remote server) and you need to edit your program from the command line, you can try the sed command. This is a stream editor which allows you to transform text in several ways. To replace tabs with spaces, try something like this: sed -i.bak $'s/\t/ /g' filename If you’re working on a Windows machine, check out sed for Windows. Avoid Python Indentation Problems Altogether To avoid this problem and remain consistent with the PEP 8 style guide, it’s also a good idea to configure your IDE to always insert 4 spaces when you hit the tab key. Configuring this is dependent on which IDE you use. For those of you not familiar with which IDEs are available, check out our article 4 Best Python IDE and Code Editors. For VSCode users, if you look on the bottom right of your window, you should see something like Spaces: 4. After clicking here, you will get the option to customize the indentation method. If your IDE of choice is PyCharm, see the documentation on Tabs and Indents to get all the information you need on configuring this option. Spyder IDE users should navigate to Tools – Preferences – Editor – Source code – Indentation characters. If you’re on an older version, it’s Tools – Preferences – Editor – Advanced settings – Indentation characters. Learning Python requires practice writing real programs. If you like learning using real-world situations, then consider taking the Python Practice: Word Games course. For some tips on how to take your Python skills past just theory, check out our article How to Practice Python Beyond ‘Hello World!’. In no time, you’ll have the skills to start implementing your own projects. For some inspiration on possible project ideas, see our article Python Coding Project Ideas for Beginners. Happy coding! Tags: python indentation