Convert Tabs to Spaces in Python

Learn practical ways to convert tabs to spaces in Python files, including the expandtabs method and command-line tools.

Before you start

Converting tabs to spaces in Python is a local text processing task. You can work with plain text files, source code files, or strings in memory. The process does not require an online service or external API. You will need Python installed on your computer and access to the file or string you want to change. A backup copy of the file is useful because tab conversion alters the original content.

Step-by-step instructions

For a string, use the expandtabs method. This method replaces tab characters with spaces based on a configurable tab size.

  • Open a Python file or start an interactive session.
  • Create a string that contains tab characters.
  • Call the expandtabs method with the desired number of spaces per tab.
  • Pass the result to a print statement or save it to a new variable.

The basic syntax is text.expandtabs(tabsize). If you do not provide a tabsize argument, the method uses a default tab stop of eight spaces. To use four spaces, write text.expandtabs(4). The method aligns text to tab stops rather than replacing every tab with the exact same number of spaces, so spacing can vary depending on the character position before each tab.

To process a file, read the file content as text, apply expandtabs, and write the modified content back. Open the source file with a context manager, read it, apply the method to the complete content, and write the result to a new file or the same file. Opening the file with the encoding parameter helps preserve non-ASCII characters in Python source files.

Check the output

Open the converted file in a text editor that can display whitespace or use a short Python script to check for remaining tab characters. Search for the tab escape sequence in the resulting string or file content. If your editor highlights tab and space characters differently, inspect the indentation visually. Keep in mind that expandtabs changes tabs according to tab stops, so the number of inserted spaces may vary from one tab to another even with the same tabsize setting.

Common problems and next steps

A common issue is expecting every tab to become the same number of spaces. expandtabs moves the next character to the next tab stop, so a tab after three characters may produce a different number of spaces than a tab after eight characters. To replace every tab with a fixed number of spaces, use a direct replacement such as text.replace('\t', ' ').

Another issue is converting a mixed file and then finding inconsistent indentation. Python requires consistent indentation within a block. After converting tabs to spaces, run the file or use a syntax checker to confirm that indentation still matches the structure of your code. If you are working on a large project, process one file at a time and compare the changed section with the backup.

For larger batch jobs, you can extend the same read, expand, and write approach to multiple files. Use a loop that processes each file in a directory, but ensure you are targeting only the files you intend to modify. Keep a backup copy before running a batch conversion so you can restore the original content if the indentation or formatting is not what you expected.