How do I write a "tab" in Python?

Question:

Let’s say I have a file. How do I write “hello” TAB “alex”?

Asked By: TIMEX

||

Answers:

You can use t in a string literal:

"hellotalex"

Answered By: Knio

It’s usually t in command-line interfaces, which will convert the char t into the whitespace tab character.

For example, hellotalex -> hello--->alex.

Answered By: Darbio

This is the code:

f = open(filename, 'w')
f.write("hellotalex")

The t inside the string is the escape sequence for the horizontal tabulation.

Answered By: Simone

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 = "0t12t24"
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"0t12t24"
print (DataString)

Returns

0t12t24

Which maybe an undesired output

String Lengths

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

DataString = "0t12t24"
print (len(DataString))

Returns

7

The raw string has a length of 9.

Answered By: CodeCupboard

As it wasn’t mentioned in any answers, just in case you want to align and space your text, you can use the string format features. (above python 2.5) Of course t is actually a TAB token whereas the described method generates spaces.

Example:

print "{0:30} {1}".format("hi", "yes")
> hi                             yes

Another Example, left aligned:

print("{0:<10} {1:<10} {2:<10}".format(1.0, 2.2, 4.4))
>1.0        2.2        4.4 
Answered By: user1767754

Here are some more exotic Python 3 ways to get “hello” TAB “alex” (tested with Python 3.6.10):

"helloN{TAB}alex"

"helloN{tab}alex"

"helloN{TaB}alex"

"helloN{HT}alex"

"helloN{CHARACTER TABULATION}alex"

"helloN{HORIZONTAL TABULATION}alex"

"hellox09alex"

"hellou0009alex"

"helloU00000009alex"

Actually, instead of using an escape sequence, it is possible to insert tab symbol directly into the string literal. Here is the code with a tabulation character to copy and try:

"hello alex"

If the tab in the string above won’t be lost anywhere during copying the string then “print(repr(< string from above >)” should print ‘hellotalex’.

See respective Python documentation for reference.

Answered By: Pavel Shishpor

Assume I have a variable named file that contains a file.
Then I could use file.write("hellotalex").

  1. file.write("hello means I’m starting to write to this file.
  2. t means a tab
  3. alex") is the rest I’m writing
Answered By: William Zeng
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.