String Methods
str.expandtabs()
Replace each tab with spaces to reach the next tab stop, counting code points since the previous newline or carriage return. Nonpositive tab sizes remove tabs. Other characters are copied; this is not terminal-aware width calculation. The original string is unchanged.
Formal Syntax & Structure
python syntaxpython
text.expandtabs(tabsize=8)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| tabsize | int | Tab-stop spacing; default 8; nonpositive removes tabs. |
Return Value
str
Text with tabs expanded into spaces.
Working Example
Example Snippetpython
print(repr("A\tB".expandtabs(4)))print(repr("AB\tC\nX\tY".expandtabs(4)))