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.
Created: February-20, 2021 | Updated: March-05, 2021
- Print Python Tab in File Using the
\t
in theprint[]
Function - Print Python Tab in the List
- Print Python Tab in the Datapoints
- Print Python Tab Using the
tab
Symbol Directly in theprint
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.
Print Python Tab in File Using the \t
in the print[]
Function
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
Print Python Tab in the List
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.
Print Python Tab in the Datapoints
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
Print Python Tab Using the tab
Symbol Directly in the print
Statement
In this method, we will use the escape sequences in the string literals to print tab
. The escape sequences could be below types.
\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