Python 3 reference · String Methods

Python String expandtabs() Method

Return a copy where tab characters are replaced by spaces, aligning to tab stops every tabsize columns (8 by default).

Python 3 Since 3.0

Syntax

str.expandtabs(tabsize=8)

Parameters

ParameterDescription
tabsize optional Columns between tab stops. Default: 8

Return value

A new string.

Examples

Tabs to spacesTry it yourself
row = "name\tage\tcity"
print(row.expandtabs())
print(row.expandtabs(6))

Output

name    age     city
name  age   city
Alignment to tab stopsTry it yourself
print(repr("a\tbc\tdef".expandtabs(4)))

Output

'a   bc  def'

Version history

VersionChange
Python 3.0Added