How to Print a Tab Character in Python
Print a tab character in Python with the \t escape sequence, align multiple values, show a literal \t, and use tabs safely in f-strings.
Use the \t escape sequence to print a tab character in Python:
print("Name\tScore")
print("Ryan\t10")
Example output:
Name Score
Ryan 10
\t represents one horizontal tab character. Its displayed width depends on the terminal, editor or other program showing the text—it is not a fixed number of spaces.
Put tabs between several values
Insert \t wherever a tab is needed:
name = "Ryan"
score = 10
print(name + "\t" + str(score))
An f-string is usually clearer:
print(f"{name}\t{score}")
The tab is part of the f-string text, between the two replacement fields.
Tabs inside f-string expressions
Python 3.12 and later accepts backslashes inside f-string replacement fields:
values = ["Python", "Sheets"]
print(f"{'\t'.join(values)}")
On Python 3.11 and earlier, that expression raises SyntaxError: f-string expression part cannot include a backslash. Assign the separator first when supporting those versions:
tab = "\t"
values = ["Python", "Sheets"]
print(f"{tab.join(values)}")
The second form works across older and current Python versions.
Print the characters \t instead of a tab
Use a raw string when the output should contain a backslash followed by t:
print(r"\t")
# \t
Alternatively, escape the backslash:
print("\\t")
# \t
Use chr(9)
The Unicode code point for a horizontal tab is 9, so chr(9) produces the same character:
print("one" + chr(9) + "two")
\t is more recognisable, but chr(9) can be useful when the character is constructed from a numeric code.
Align columns reliably
Tabs move to the next tab stop, so values of different lengths may not form neat columns:
print("A\t100")
print("Long name\t20")
Use formatted field widths when alignment must be predictable:
print(f"{'Name':<12}{'Score':>6}")
print(f"{'Ryan':<12}{10:>6}")
For the difference between a tab character and spaces, see how many spaces a tab represents.