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.
Contrary to PEP-8 [//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 |
I can verify this on 3.7.4. |
I believe that the interpreter only requires that each block is consistent, not that all blocks in a module are consistent. |
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. |
Seems related issue24260 |
Thanks, Serhiy. This has been reported in 24260, so I'm closing this issue. |
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 |
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 |