How do you insert a tab in python?

The Python reference manual includes several string literals that can be used in a string. These special sequences of characters are replaced by the intended meaning of the escape sequence.

Here is a table of some of the more useful escape sequences and a description of the output from them.

Escape Sequence       Meaning
\t                    Tab
\\                    Inserts a back slash (\)
\'                    Inserts a single quote (')
\"                    Inserts a double quote (")
\n                    Inserts a ASCII Linefeed (a new line)

Basic Example

If i wanted to print some data points separated by a tab space I could print this string.

DataString = "0\t12\t24"
print (DataString)

Returns

0    12    24

Example for Lists

Here is another example where we are printing the items of list and we want to sperate the items by a TAB.

DataPoints = [0,12,24]
print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))

Returns

0    12    24

Raw Strings

Note that raw strings (a string which include a prefix "r"), string literals will be ignored. This allows these special sequences of characters to be included in strings without being changed.

DataString = r"0\t12\t24"
print (DataString)

Returns

0\t12\t24

Which maybe an undesired output

String Lengths

It should also be noted that string literals are only one character in length.

DataString = "0\t12\t24"
print (len(DataString))

Returns

7

The raw string has a length of 9.

How do you print a tab character in Python?

The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t'. To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.

Here’s a simple example:

>>> my_tabbed_string = 'Space\tman'
>>> print(my_tabbed_string)
Space	man

What if you’d prefer to see the tab character instead of the actual spacing?

If in the REPL just return the variable containing the tabbed string on a new line, like so:

>>> my_tabbed_string
'Space\tman'

You can use the shortcut form of the tab character in most places, but you cannot use any backslash character in an f-string expression (the commands between the curly braces {}.

For example, using the following produces a SyntaxError:

>>> print(f"{str(1) + '\t' + str(2)")
  File "", line 1
SyntaxError: f-string expression part cannot include a backslash

There are a couple of ways to get around this limitation (besides not using slashes at all!).

As demonstrated in the post where I use tabs to print a list, you can place the tab character into a variable and reference the “tab variable” in the f-string expression, like so:

>>> tab = "\t"
>>> print(f"{str(1) + tab + str(2)")
1	2

Using chr() Built-In Function

An alternative approach to the shorthand method is using the built-in chr() function.

The chr() function takes one parameter, an integer ranging from 0 to 1,114,111, with each number in that range representing a Unicode character.

To find out what the integer representation of the tab character is, you can use another built-in function ord() which provides the integer representation of a Unicode character. Using it and confirming like so:

>>> ord('\t')
9
>>> chr(9)
'\t'

As you can see chr(9) represents the tab character. Therefore, another way to print the tab character is to use the chr(9) function as this produces the same results:

>>> print(f"{str(1) + chr(9) + str(2)")
1	2

Summary

To print the tab character use the abbreviated shorthand method of '\t' or chr(9) if using backslashes in your context will not work.

Next, you might want to read another post about how many spaces is a tab?

  1. HowTo
  2. Python How-To's
  3. Print Tab in Python

Created: February-20, 2021 | Updated: March-05, 2021

  1. Print Python Tab in File Using the \t in the print() Function
  2. Print Python Tab in the List
  3. Print Python Tab in the Datapoints
  4. Print Python Tab Using the tab Symbol Directly in the print Statement

The '\' backslash in Python strings is a special character, sometimes called the escape character. It is used to represent whitespace characters as '\t' represents a tab.

This article will discuss some methods to print the Python tab.

We can use \t in the print() function to print the tab correctly in Python.

The complete example code is given below.

print("Python\tprogramming")

Output:

Python	Programming

This method will insert tabs between different elements of the list.

The complete example code is given below:

Lst = ['Python','Java','C++']
print (str(Lst[0]) + "\t" + str(Lst[1]) + "\t" + str(Lst[2]))

Output:

Python	Java	C++

str will convert the list elements into the string with all values separated by Python tabs.

This method will print tabs in the given statement or some data points.

The complete example code is given below:

Data_Points = "2\t6\t10"
print (Data_Points)

Output:

2	6	10

In this method, we will use the escape sequences in the string literals to print tab. The escape sequences could be below types.

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

The name of the table in the Unicode database is TAB or tab, or TaB because it is not case insensitive.

The other names representing the tab in the Unicode database are HT, CHARACTER TABULATION, and HORIZONTAL TABULATION.

The tab’s Unicode value is 09 for \x, 0009 for \u, and 00000009 for \U.

The complete example code is given below:

print("python\N{TAB}programming")
print("python\N{tab}programming")
print("python\N{TaB}programming")

print("python\N{HT}programming")
print("python\N{CHARACTER TABULATION}programming")
print("python\N{HORIZONTAL TABULATION}programming")

print("python\x09programming")
print("python\u0009programming")
print("python\U00000009programming")

Output:

python	programming
python	programming
python	programming
python	programming
python	programming
python	programming
python	programming
python	programming
python	programming

Related Article - Python Print

  • Print Multiple Arguments in Python
  • Print With Column Alignment in Python
  • Print Subscripts to the Console Window in Python
  • Print Quotes in Python
  • How do you insert a tab in python?

    Can I use tab in Python?

    Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

    How do I print a tab in Python?

    How do you print a tab character in Python? The easiest way to print a tab character in Python is to use the short-hand abbreviation '\t' . To see the tab spaced character in the REPL wrap any variable containing a tab character in the built-in print() function.

    What is the Tab key in Python?

    The Python tab provides auto-completion so that you can quickly finish statement tokens. If you write a partial token but are unsure how to complete it or want to fill in the token automatically, press Tab while the cursor is still in the input pane.

    How do I use the tab button in Python?

    The action of the tab key depends on the Editor > Keyboard > Personality preference, the file type being edited, and the position within the file. To insert a real tab character, press Ctrl-T.