Print Tab in Python

This tutorial will cover the different ways of printing tabs in Python.

 

Python Print Tab Example

To print a tab in Python, use the \ (backslash) escape character followed by a t within a print() function like this:

 

print('Hello\tworld.')
Hello  world.

 

Python List Print with Tabs Example

Here is an example of how to print a list with each element separated by a tab:

 

items = ['Apple','Pear','Banana']
print (str(items[0]) + "\t" + str(items[1]) + "\t" + str(items[2]))
Apple	Pear	Banana

 

Here are the ways to print a tab directly using unicodes:

 

  • \N{name} – name is the Character name in the Unicode database
  • \uxxxx – 16-bit Unicode
  • \Uxxxxxxxx – 32-bit Unicode
  • \xhh  – 8-bit Unicode

 

print("hello\N{TAB}world")
print("hello\N{tab}world")
print("hello\N{TaB}world")
hello	world
hello	world
hello	world
print("hello\N{HT}world")
print("hello\N{CHARACTER TABULATION}world")
print("hello\N{HORIZONTAL TABULATION}world")
hello	world
hello	world
hello	world
print("hello\x09world")
print("hello\u0009world")
print("hello\U00000009world")
hello	world
hello	world
hello	world