Write a Tab in Python

This article discusses how to represent a tab character in Python and some used cases.

The Tab Character in Python

The tab character in Python is represented by the escape sequence “\t”, as shown below.

Other ways to represent tab characters in Python include:

Now, we can discuss some used cases for a tab in Python. We will use the “\t” in most of the coming examples, but you can use any character sequence given above.

Used Cases for the Tab Character in Python

There are several cases you can use the tab in Python. These include:

Example 1: Using a tab within a string

Anytime the Python interpreter finds the sequence “\t”, it inserts a tab character at that point. Here is an example.

Example 2: Using tab with string formatting

You can also pass a tab character into a string through f-string formatting (available on Python 3.6 and later) or string.format() function.

For f-string, we cannot pass the “\t” character directly because f-string formatting does not accept any backslash within the placeholder(s). For example,


SyntaxError: f-string expression part cannot include a backslash

You can navigate that problem by assigning the “\t” character into a variable and then using it in the placeholder. That is,

Alternatively, as shown below, you can use chr(9) as a tab character on the placeholders.

You can also concatenate a tab character with other strings, as shown in the example below.

Example 3: Using a tab to join items of an object

We can join elements of an iterable using the “\t” character using the join() function, as shown below.

Example 4: Using a tab to separate items in the print statement

The print statement contains the sep argument that is used to separate the values in the output. The default value for “sep” is a space character. You can use the tab character by setting it to the “\t” sequence.


The “\t” character sequence is Python’s commonly used tab character. In this article, we discussed more character sequences you could use to represent a tab and some widely used cases for tab characters.