Python String expandtabs() Method returns a copy of the string in which tab characters ie. ‘\t’ are expanded using spaces, optionally using the given tabsize (default 8).
The syntax of the method is: str.expandtabs(tabsize=8)
Key Points :
• The expandtabs() function of the Python string creates space between strings in runtime. It replaces the ‘\t’ character with the space specified; the default size is 8.
• An integer type value can be the sum of size replacing ‘\t.’
• When a non-integer type value is passed on a TypeError exception is raised.
• Expandtabs() appears to be an essential technique for dealing with the inclusion of spaces during operation.
Return Value from expandtabs()
The expandtabs() returns a string that replaces all ‘\t’ characters with whitespace characters before the next tabsize parameter is set.
Below is the python program to demonstrate the expandtabs() function:
str = "The Best Learning Resource for \tOnline Education" print (str) print (str.expandtabs(20)) print (str.expandtabs(42))
When we run above the program, the outcome is as follows:
The Best Learning Resource for Online Education
The Best Learning Resource for Online Education
The Best Learning Resource for Online Education
Below are several other functions that we can use to work with string in Python 3