Open In App

How to Print a Tab in Python

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, printing a tab space is useful when we need to format text, making it more readable or aligned. For example, when displaying data in columns, we might want to add a tab space between the values for a cleaner appearance. We can do this using simple methods like \t, the print() function or by using the str.format() method.

Using the Special Character \t

The easiest and most common way to print a tab space is by using the special character \t. This represents a tab space in Python.

Python
# Using \t to print a tab
a = "Hey"
b = "Geek"
print(a , "\t" , b)

Output
('Hey', '\t', 'Geek')

The print() function in Python accepts a 'sep' parameter in which specifies the separator between the multiple values passed to the function. By default 'sep' is set to the ' ' (space) but we can change it to '\t' to the insert tabs between the values.

Python
# Using sep in print to insert a tab
a = "Hello"
b = "Geek"
print(a, b, sep="\t")

Output
Hello	Geek

Using String Concatenation

We can also print a tab by string concatenation that contains \t with other strings.

Python
# Using \t directly in print
a = "Geek"
b = "Rocks"
print(a + "\t" + b)

Output
Geek	Rocks

Using str.format()

If we want more control over formatting, we can use str.format() to insert a tab between values.

Python
# Using str.format() for tab space
a = "Geeky"
b = "Fun"
print("{}\t{}".format(a, b))

Output
Geeky	Fun

Next Article
Practice Tags :

Similar Reads