Remove the tabs from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Remove the tabs from a String in Python #

To remove the tabs from a string:

  1. Use the split() method to split the string on each whitespace.
  2. Use the join() method to join the string with a space separator.
  3. The new string won't contain any tabs.
main.py
my_str = 'first\tsecond\tthird' # ✅ replaces one or more, consecutive tabs or spaces with single space result_1 = ' '.join(my_str.split()) print(repr(result_1)) # 👉️ 'first second third' # ✅ remove tabs from the string result_2 = my_str.replace('\t', '') print(repr(result_2)) # 👉️ firstsecondthird # ✅ replace tabs with a space in the string result_3 = my_str.replace('\t', ' ') print(repr(result_3)) # 👉️ 'first second third' # ✅ remove only leading and trailing whitespace (including tabs) my_str_2 = '\tfirst\tsecond\t' result_4 = my_str_2.strip() print(repr(result_4)) # 👉️ 'first\tsecond'

We used the str.split() method to split the string on one or more consecutive tabs or spaces.

main.py
my_str = 'first\tsecond\tthird' print(my_str.split()) # 👉️ ['first', 'second', 'third']

The str.split() method splits the string into a list of substrings using a delimiter.

If no delimiter is provided, the split() method splits the string on one or more whitespace characters (including tabs).

The last step is to join the list of strings into a string with a space separator.

main.py
my_str = 'first\tsecond\tthird' result = ' '.join(my_str.split()) print(repr(result)) # 👉️ 'first second third'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We called the method on a string containing a space to join the list of strings with a space separator.

Alternatively, you can use the replace() method.

Use the str.replace() method to remove the tabs from a string, e.g. result = my_str.replace('\t', ''). The replace method will remove the tabs from the string by replacing them with empty strings.

main.py
my_str = 'first\tsecond\tthird' # ✅ remove tabs from the string result_1 = my_str.replace('\t', '') print(repr(result_1)) # 👉️ firstsecondthird # ✅ replace tabs with a space in the string result_2 = my_str.replace('\t', ' ') print(repr(result_2)) # 👉️ 'first second third'

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.

If you only need to remove the leading and trailing tabs from a string, use the strip() method.

main.py
my_str = '\tfirst\tsecond\t' result = my_str.strip() print(repr(result)) # 👉️ 'first\tsecond'

The str.strip method returns a copy of the string with the leading and trailing whitespace (including the tabs) removed.

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.