Python String expandtabs() Method

Python String Expandtabs() Method

This is the Python String expandtabs() method Tutorial. Learn to get the copy of a given string with all the tab chars replaced with whitespace characters.

Python String expandtabs()

The Python String expandtabs() method returns a new copy of the given string in which all of the tab characters are replaced with the whitespace characters until the next multiple of the argument provided.

This method is quite useful in printing out the tabular information to the console screen.

Note. The above brief explanation may confuse most people. Therefore, I highly recommend you to have a good reading of this article to better understand the purpose, working and the usage of the expandtabs() method.

Syntax

Given below is the syntax of the string.expandtabs() method.

expandtabs() Parameters

This method takes only a single argument and that is the tabSize. It is the multiple according to which the number of whitespace a particular '\t' character is to be replaced is calculated.

It is optional to provide the tabSize argument. If you will not pass this argument to the string.expandtabs() method, it will automatically assume the default value of this argument i.e. 8.

expandtabs() Return Value

It returns a new string that is the original string on which this method is applied with each of the '\t' characters or the tab spaces are replaced with the particular calculated number of whitespace using the tabSize argument.

Working of string.exapandtabs()

What matters for this method is the index of the \t character in the string. Let’s try to understand the working of this method with the help of a quick example string.

exampleString = "abc\tdefg\tghi"

In the above string, the index of the first \t character is 3 as the index of the first character in any python string is 0. Let call it indexOfTabCharacter.

Let’s say we’re taking the default tabSize of 8. Now calculate, tabSize - indexOfTabCharacter. i.e. 8 - 3 = 5.

Hence, the first \t character located at index 3 will be replaced by 5 whitespaces if using the method expandedtabs() with tabSize 8.

Now, 8 is the first tab stop. The next tab stops will be the multiples of 8 only, i.e. 16, 24, 32, 40, 48 and so on.

Also, as the first tab character is replaced with the 3 whitespaces, the updates string is as defined below.

exampleString = "abc     defg\tghi"

Now, the index of the next tab character is 12 and the next tab stop is 16 as the second multiple of 8 is 16. And 16 - 12 = 4. Therefore, this '\t' character will be replaced by 4 whitespaces.

Hence, the working of the expandtabs() method is justified.

Examples

Given below are the two examples demonstrating the usage of the Python String expandtabs() method.

Example 1. Working of the expandtabs() method without the tabSize argument

In the following example, we have a string with '\t' characters. We’ve applied the expandtabs() method on this string without specifying any argument. As the default value of the tabSize argument is 8, hence you can observe in the output, the first tab character is replaced with 5 whitespaces while the second one is replaced with 4 whitespaces as justified in the above section explaining the working of this method.

#A string with \t characters
line = "abc\tdefg\thij?"

#Applying expandtabs() on line string
#Without passing the tabSize argument value
newString = line.expandtabs()

print(newString)

Output.

abc     defg    hij?

Example 2. Working of the string.expandtabs() method with different arguments

In this example also, we’re taking the same string but now we’re taking the different argument. To help you better understand how the method is working, the explanation for each of the cases is also given after the output.

#A string with \t characters
line = "abc\tdefg\thij?"

#Applying expandtabs()
#With Different tabSize argument Value in each case

print("Original String:", line)

#Case 1 - Tab Size of 1
newString = line.expandtabs(1)
print("Tab Size of 1:", newString)

#Case 2 - Tab Size of 2
newString = line.expandtabs(2)
print("Tab Size of 2:", newString)

#Case 3 - Tab Size of 3
newString = line.expandtabs(3)
print("Tab Size of 3:", newString)

#Case 4 - Tab Size of 4
newString = line.expandtabs(4)
print("Tab Size of 4:", newString)

#Case 5 - Tab Size of 10
newString = line.expandtabs(10)
print("Tab Size of 10:", newString)

#Case 6 - Tab Size of 15
newString = line.expandtabs(15)
print("Tab Size of 15:", newString)

Output.

Original String: abc   defg    hij?
Tab Size of 1: abc defg hij?
Tab Size of 2: abc defg  hij?
Tab Size of 3: abc   defg  hij?
Tab Size of 4: abc defg    hij?
Tab Size of 10: abc       defg      hij?
Tab Size of 15: abc            defg           hij

Explanation For Each Case.

Case 1. The tabSize is 1. Without any logic, you can simply assume here that every '\t' character will be replaced by a single whitespace.

Case 2. The tabSize is 2. Therefore, the tab stops will be at 2, 4, 6, 8, 10 and so on. The first '\t' character is at index 3 and the upcoming tab stop is 4, therefore, 4 - 3 = 1. Hence, the first '\t' character is replaced with a single whitespace. The next tab character is now at the updated index of 8 and the next tab stop is 10 and 10 - 8 = 2. Therefore, this tab character will be replaced with 2 whitespaces.

Case 3. The tabSize is 3. Therefore, the tab stops will be at 3, 6, 9, 12, 15 and so on. The first '\t' character is at index 3 and the upcoming tab stop is 6, therefore, 6 - 3 = 3. Hence, the first '\t' character is replaced with 3 whitespaces. The next tab character is now at the updated index of 10 and the next tab stop is 12 and 12 - 10 = 2. Therefore, this tab character will be replaced with 2 whitespaces.

Case 4. The tabSize is 4. Therefore, the tab stops will be at 4, 8, 12, 16, 20 and so on. The first '\t' character is at index 3 and the upcoming tab stop is 4, therefore, 4 - 3 = 1. Hence, the first '\t' character is replaced with a single whitespace. The next tab character is now at the updated index of 8 and the next tab stop is 12 and 12 - 8 = 4. Therefore, this tab character will be replaced with 4 whitespaces.

Note. If the updated index of the tab character and the next tab stop is the same, you should take the next tab stop. As in Case 4, the updated index of the second tab character is 8 and also 8 is a tab stop, hence we have taken the next tab stop 12 for subtraction.

Case 5. The tabSize is 10. Therefore, the tab stops will be at 10, 20, 30, 40, 50 and so on. The first '\t' character is at index 3 and the upcoming tab stop is 10, therefore, 10 - 3 = 7. Hence, the first '\t' character is replaced with 7 whitespaces. The next tab character is now at the updated index of 14 and the next tab stop is 20 and 20 - 14 = 6. Therefore, this tab character will be replaced with 6 whitespaces.

Case 6. The tabSize is 15. Therefore, the tab stops will be at 15, 30, 45, 60, 75 and so on. The first '\t' character is at index 3 and the upcoming tab stop is 15, therefore, 15- 3 = 12. Hence, the first '\t' character is replaced with 12 whitespaces. The next tab character is now at the updated index of 19 and the next tab stop is 30 and 30 - 19 = 11. Therefore, this tab character will be replaced with 11 whitespaces.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *