How do you print tabs in python 3?

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.

  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 print tabs in python 3?

    How do you print a tab 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. ')

    How do you give a tab space in Python 3?

    In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

    How do you print multiple tabs in Python?

    You can directly use the escape sequence “ \t ” tab character to print a list tab-separated in Python.

    How do I print a tab?

    Open the data into which you wish to insert tab paper, and then select the settings..
    Click the [Special Modes] tab..
    Select [Tab Paper Print] and click the [Settings] button..
    Select the tab position settings. ... .
    Select the page settings..