Tab space 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.

Created on 2019-10-16 08:00 by Mikko Rantalainen, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg354779 - (view)Author: Mikko Rantalainen (Mikko Rantalainen)Date: 2019-10-16 08:00
Contrary to PEP-8 (https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) claiming "Python 3 disallows mixing the use of tabs and spaces for indentation", in reality Python 3 allows mixing tabs and spaces without warnings or errors. For example (with all spaces replaced with middle dot and all spaces replaced with a short arrow):

#!/usr/bin/python3
for·i·in·range(2):
··for·j·in·range(3):
➧··for·k·in·range(4):
·➧···print(i,j,k,sep=",")

Either the PEP-8 should be fixed or the parser/compiler should be fixed. 

The current behavior seems a bit unstable because the exact order of spaces and tabs causes "TabError: inconsistent use of tabs and spaces in indentation" to appear more or less random.

I'd prefer python3 to require that *all whitespace* at the start of the all the lines is tabs for the whole file or spaces for the whole file. And first indented line sets the preference for the whole file.

(Personally I'd prefer tabs contrary to PEP-8 language but this bug is not about the preference. This is PEP-8 claiming things that are not true.)

$ python3 --version
Python 3.5.2
msg354780 - (view)Author: Eric V. Smith (eric.smith) *
Tab space in python 3
Date: 2019-10-16 08:05
I can verify this on 3.7.4.
msg354788 - (view)Author: Steven D'Aprano (steven.daprano) *
Tab space in python 3
Date: 2019-10-16 08:57
I believe that the interpreter only requires that each block is consistent, not that all blocks in a module are consistent.
msg354791 - (view)Author: Serhiy Storchaka (serhiy.storchaka) *
Tab space in python 3
Date: 2019-10-16 09:25
Thank you for your report Mikko. Yes, the heuristic used in Python 3 is unable to catch all inconsistencies in using tabs and spaces.

Such bug was already reported early. There should be an open issue.
msg354795 - (view)Author: Karthikeyan Singaravelan (xtreak) *
Tab space in python 3
Date: 2019-10-16 11:30
Seems related issue24260
msg354796 - (view)Author: Eric V. Smith (eric.smith) *
Tab space in python 3
Date: 2019-10-16 11:32
Thanks, Serhiy. This has been reported in 24260, so I'm closing this issue.
History
DateUserActionArgs
2022-04-11 14:59:21 admin set github: 82677
2019-10-16 11:32:00 eric.smith set status: open -> closed
superseder: TabError behavior doesn't match documentation
messages: + msg354796

type: behavior
resolution: duplicate
stage: resolved

2019-10-16 11:30:15 xtreak set nosy: + xtreak
messages: + msg354795
2019-10-16 09:25:58 serhiy.storchaka set nosy: + serhiy.storchaka
messages: + msg354791
2019-10-16 08:57:31 steven.daprano set nosy: + steven.daprano
messages: + msg354788
2019-10-16 08:05:23 eric.smith set nosy: + eric.smith
messages: + msg354780
2019-10-16 08:00:10 Mikko Rantalainen create

How many spaces is a tab in Python 3?

Python 3 says: Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix).

How much spaces is a tab in Python?

In Java, a tab is two spaces, while in Python, it's one.

How do you print tabs in Python 3?

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.

How do you give a tab two spaces in Python?

Python String expandtabs() The expandtabs() method returns a copy of string with all tab characters '\t' replaced with whitespace characters until the next multiple of tabsize parameter.