Open In App

How to Print a Tab in Python: Enhancing Text Formatting

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Using ‘\t’ Escape Sequence
  • Using ‘sep’ Parameter
  • Using f-strings (Python 3.6+)

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.

Python
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.

Python3
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.

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

Output
Name:    kumar
Age:    30

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads