Open In App

How to Print a Tab in Python: Enhancing Text Formatting

Python provides various formatting options to enhance the appearance of the text output. One common requirement is to insert tabs into printed text for better alignment or readability. This article explores different methods to print tabs in Python allowing developers to format text effectively.

How to Print a Tab in Python: Enhancing Text Formatting

Below are the methods of printing a tab in Python:

Print a Tab in Python Using '\t' Escape Sequence

In this example, we are printing a tab in Python by using the '\t' escape sequence within the string. When Python encounters '\t' in a string, it inserts a tab character.

print("Name:\tKumar")
print("Age:\t20")

Output
Name:    Kumar
Age:    20

Print a Tab in Python Using 'sep' Parameter

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 you can change it to '\t' to the insert tabs between the values.

print("Name:", "Kumar", sep="\t")
print("Age:", 25, sep="\t")

Output
Name:    Kumar
Age:    25

Print a Tab in Python Using f-strings (Python 3.6+)

The f-strings provide a concise and readable way to the format strings in Python. we can directly embed tab characters within f-strings. In this example, we are using f-strings for print tab in Python.

name = "kumar"
age = 30
print(f"Name:\t{name}\nAge:\t{age}")

Output
Name:    kumar
Age:    30
Article Tags :