AppDividend
Latest Code Tutorials

Python String expandtabs() Method Example

0

In Python String expandtabs() method is used to deal with the spaces in a string. Sometimes it is required to specify the space in a string. For these, if we don’t use expandtabs(), we’ll have to edit the string every time, which is very time-consuming.

If you want to get rid of this problem, python has introduced its library expandtabs(), which specifies the amount of space to be given in that position in the string where we have used the “\t” symbol in that particular string.

By default, space is of 8 characters if we don’t give any parameters.

Python String expandtabs()

Sometimes, there is a need for specifying the space in the string, but the amount of space to be left is uncertain and depending upon the environment and conditions.

For these cases, the need to modify the string, again and again, is a tedious task. Hence python in its library has “expandtabs(),” which specifies the amount of space to be substituted with the “\t” symbol in the string.

Python expandtabs() method returns a copy of the string with all tab the characters ‘\t’ replaced with the whitespace characters until the next multiple of tabsize parameter.

Syntax

string.expandtabs(size)

See the following example.

# app.py

data = "Hello/tComputer"
print(data.expandtabs(6))

See the output.

Hello     Computer

Parameters

Python expandtabs() method takes only one parameter that is the size which specifies the number of space we want to give at the place of the “\t” symbol.

Return Value

It returns the string in which space replaces the tabs.

Example programs on expandtabs() method

Write the program to show the mechanism of expandtabs().

# app.py

string = "Hello\tAlien\tHere."
print("Hello I am going to give 2 spaces between every word using expandtabs()")
print(string.expandtabs(2))

See the output.

➜  pyt python3 app.py
Hello I am going to give 2 spaces between every word using expandtabs()
Hello Alien Here.
➜  pyt

Example2: Write a program in which there will be two strings one without using any parameter in expandtabs() and other with the parameter.

# app.py

string = "Hello\tI\tam\tat\twork."
print("Printing the string without any parameter:")
print(string.expandtabs())
print("Printing the string using parameter of size=2")
print(string.expandtabs(2))

See the output.

➜  pyt python3 app.py
Printing the string without any parameter:
Hello   I       am      at      work.
Printing the string using parameter of size=2
Hello I am  at  work.
➜  pyt

#Python 3 expandtabs() With Different Argument

See the following examples.

# app.py

str = "el\t1121\tmike"
print('Original String:', str)

# tabsize is set to 2
print('Tabsize 2:', str.expandtabs(2))

# tabsize is set to 3
print('Tabsize 3:', str.expandtabs(3))

# tabsize is set to 4
print('Tabsize 4:', str.expandtabs(4))

# tabsize is set to 5
print('Tabsize 5:', str.expandtabs(5))

# tabsize is set to 6
print('Tabsize 6:', str.expandtabs(6))

See the output.

➜  pyt python3 app.py
Original String: el	1121	mike
Tabsize 2: el  1121  mike
Tabsize 3: el 1121  mike
Tabsize 4: el  1121    mike
Tabsize 5: el   1121 mike
Tabsize 6: el    1121  mike
➜  pyt

The default tabsize is 8. The tab stops are 8, 16, and so on. Hence, there are 5 spaces after ‘el’ and 3 after ‘1121’ when you print the original string.

When you set the tabsize to 2, the tab stops are 2, 4, 6, 8, and so on. For ‘el’, the tab stop is 4, and for ‘1121’, the tab stop is 10. Hence, there is 1 space after ‘el’ and 1 space after ‘1121’.

When you set the tabsize to 3, the tab stops are 3, 6, 9, and so on. For ‘el’, the tab stop is 6, and for ‘1121’, the tab stop is 12. Hence, there are 3 spaces after ‘el’ and 1 space after ‘1121’.

When you set the tabsize to 4, the tab stops are 4, 8, 12, and so on. For ‘el’, the tab stop is 4, and for ‘1121’, the tab stop is 12. Hence, there is 1 space after ‘el’ and 3 spaces after ‘1121’.

When you set the tabsize to 5, the tab stops are 5, 10, 15, and so on. For ‘el’, the tab stop is 5, and for ‘1121’, the tab stop is 15. Hence, there are 2 spaces after ‘el’ and 5 spaces after ‘1121’.

When you set the tabsize to 6, the tab stops are 6, 12, 18, and so on. For ‘el’, the tab stop is 6, and for ‘1121’, the tab stop is 12. Hence, there are 3 spaces after ‘el’ and 1 space after ‘1121’.

Finally, Python String expandtabs() Method Example is over.

Recommended Posts

Python string center()

Python object() function

Python string endswith()

Python dict() function

Python hex() function

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.