The expandtabs function in Python substitutes all tab characters with a designated number of spaces. By default, each tab is converted into 8 spaces, although this can be adjusted based on specific needs.
We have the capability to provide values such as 1, 2, 4, or higher to the method, which will substitute the tab character with the specified count of space characters.
Python String expandtabs Method Syntax
It has the following syntax:
expandtabs(tabsize=8)
Parameters
- tabsize: This parameter is optional, with a default setting of 8.
Return Type
It returns a modified string.
Different Examples for Python String expandtabs Method
Let us examine a few instances to gain a better understanding of the expandtabs function.
Python String expandtabs Method Example 1
Invoking a method without indicating any spaces will assign it to its preset default value.
# Python endswith() function example
# Variable declaration
str = "Welcome \t to \t the \t ExampleTech."
# Calling function
str2 = str.expandtabs()
# Displaying result
print(str2)
Output:
Welcome to the ExampleTech.
Python String expandtabs Method Example 2
Observe how the output varies with each invocation. Each method is configured to utilize a distinct number of spaces.
# Python endswith() function example
# Variable declaration
str = "Welcome \t to \t the \t ExampleTech."
# Calling function
str2 = str.expandtabs(1)
str3 = str.expandtabs(2)
str4 = str.expandtabs(4)
# Displaying result
print(str2)
print(str3)
print(str4)
Output:
Welcome to the ExampleTech.
Welcome to the ExampleTech.
Welcome to the ExampleTech.