str.expandtabs() Expand tabs
Last updated: 2026-09-22
str.expandtabs() Expand tabs
str.expandtabs(tabsize=8) replaces tabs with spaces, aligning to the next tabsize boundary.
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.expandtabs(tabsize=8)
⚙️ Parameters
| tabsize Optional | Number of spaces per tab stop; defaults to 8.(Default:8) |
|---|
Returns:str. A copy with all tab characters expanded to spaces.
Mutates the original:No (returns a new object)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
a b c
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does expandtabs() do?
AIt replaces \t in the string with spaces so that the next tab stop lands on a column that is a multiple of tabsize, with tabsize defaulting to 8.
QHow is tabsize=4 calculated?
AEach tab stop occupies 4 columns. In 'a\tb'.expandtabs(4), 'a' is in column 1 and the next boundary of 4 is column 5, so 3 spaces are added.
QWhat happens if the string has no tabs?
AA copy of the original string is returned with the content unchanged; an empty string returns an empty string without raising an error.