Replace tabs with spaces in a File or String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Replace tabs with spaces in a File or String in Python #

Use the str.replace() method to replace tabs with spaces, e.g. result = my_str.replace('\t', ' '). The str.replace() method will return a new string where each occurrence of a tab is replaced with a space.

main.py
my_str = 'one\ttwo\tthree' # ✅ Replace tabs with spaces in a string result = my_str.replace('\t', ' ') print(repr(result)) # 👉️ 'one two three' # ----------------------------------------- # ✅ Replace tabs with spaces in a file with open('example.txt', 'r', encoding='utf-8') as input_file: lines = input_file.readlines() print(lines) with open('example.txt', 'w', encoding='utf-8') as output_file: for line in lines: output_file.write(line.replace('\t', ' ')) # ----------------------------------------- # ✅ Replace tabs with spaces in list of strings my_list = ['a\tb', 'c\td', 'e\tf'] new_list = [item.replace('\t', ' ') for item in my_list] print(new_list) # 👉️ ['a b', 'c d', 'e f']

We used the str.replace() method to replace tabs with spaces.

The first example uses the method to replace tabs with spaces in a string.

main.py
my_str = 'one\ttwo\tthree' result = my_str.replace('\t', ' ') print(repr(result)) # 👉️ 'one two three'

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

To replace tabs with spaces in a file:

  1. Open the file in read mode and read its contents.
  2. Open the file in write mode.
  3. Use the str.replace() method to replace the tabs on each line with spaces.
main.py
with open('example.txt', 'r', encoding='utf-8') as input_file: lines = input_file.readlines() print(lines) with open('example.txt', 'w', encoding='utf-8') as output_file: for line in lines: output_file.write(line.replace('\t', ' '))

Make sure to update the name of the file.

We iterate over the lines in the file and use the str.replace() method to replace the tabs with spaces on each line.

The with statement automatically takes care of closing the file for us.

Replace tabs with spaces in a List of strings #

To replace the tabs with spaces in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to replace the tabs with spaces for each item.
  3. The items in the new list will contain spaces instead of tabs.
main.py
my_list = ['a\tb', 'c\td', 'e\tf'] new_list = [item.replace('\t', ' ') for item in my_list] print(new_list) # 👉️ ['a b', 'c d', 'e f']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.replace() method to replace the tabs in the current item with spaces.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.