Menu Close

Python String expandtabs() Method

python string expandtabs

In this article, you will learn about the Python string expandtabs() method. Python string expandtabs method is used with string. In Python, the string expandtabs function sets the tab size to the specified number of whitespaces.

In previous tutorial, we have seen Python string endswith() method.

Python string expandtabs() method:

The string expandtabs function in python is a string built-in method which only used with string.
Python string expandtabs() method return the copy of the string with all tabs characters and replace them with whitespace.

OR

The expandtabs() method sets the tab size to the specified number of whitespaces.

Syntax:


The syntax of the string expandtabs function is:

string.expandtabs(tabsize)

Parameter:

The string expandtabs function in python accept only one parameter that is tabsize.

tabsize – ( Optional ) A number specify the tab size. By default tabsize is 8.

Let’s see an example for using python expandtabs() string method.

Python string expandtabs() Method Example:

Here we will go through various examples so that you can understand the python string expandtabs method in a better way.

Example 1:

Here we will use string expandtabs() method without parameter.

str = "H \te\tl\tl\to"
print("Old string:- ", str)

str2 = str.expandtabs()
print(str2)

Output

Old string:-  H         e       l       l       o
H       e       l       l       o

Example 2:

Here we will use string expandtabs() method with parameter.

str = "\tProgramming Funda"
print("Old string:- ", str)

str2 = str.expandtabs(4)
print(str2)

Output

Old string:-    Programming Funda
    Programming Funda

Example 3:

Here we will use string expandtabs() method with various parameter.

str = "\tPython"
print("Old string:- ", str)

print("Tab Size 4: ", str.expandtabs(4))
print("Tab Size 6: ", str.expandtabs(6))
print("Tab Size 8: ", str.expandtabs(8))
print("Tab Size 10: ", str.expandtabs(10))
print("Tab Size 12: ", str.expandtabs(12))

Output

Old string:-    Python
Tab Size 4:      Python
Tab Size 6:        Python
Tab Size 8:          Python
Tab Size 10:            Python
Tab Size 12:              Python

Conclusion:

In this tutorial, you have learned the Python string expandtabs method to return the copy of the string with all tabs characters. string expandtabs function in python is a built-in string function.

I hope this tutorial will help you. if you like this article, please share with your friends who want to learn Python programming from scratch to advanced.

For More Information:- Click Here

Python String count() Method
Python String find() Method

Related Posts