Python String expandtabs()
Python String expandtabs() method returns the number of occurrences of a specified value in a string.
In this tutorial, we will learn the syntax and examples for expandtabs() method of String class.
Syntax
The syntax of String expandtabs() method in Python is
</>
Copy
str.expandtabs(tabsize)
where
Parameter | Required/ Optional | Description |
---|---|---|
tabsize | Optional | An integer. It specifies the size for a tab. The default tab size is 8. |
Example
In this example, we will take a string 'abc\tde\tf'
, and expand the tabs with tab size of 5.
Python Program
</>
Copy
x = 'abc\tde\tf'
tabsize = 5
result = x.expandtabs(tabsize)
print(result)
Output
abc de f
Explanation
abc\tde\tf input string
abc de f output string
||||| tabsize of 5
||||| tabsize of 5
expandtabs() with Default tabsize
In this example, we will take a string 'abc\tde\tf'
, and expand the tabs with default tab size of 8.
Python Program
</>
Copy
x = 'abc\tde\tf'
result = x.expandtabs()
print(result)
Output
abc de f
Explanation
abc\tde\tf input string
abc de f output string
|||||||| tab size of 8
|||||||| tab size of 8
Conclusion
In this Python Tutorial, we learned how to expand the tabs in given string with specific tab size using String method – expandtabs().