You should take a look at String Literal document, which says:
The backslash (\
) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter r' or
R'; such strings are called raw strings and use different rules for backslash escape sequences.
In your example string, \t
are not two characters but a single character which represents ASCII Horizontal Tab (TAB).
In order to tell your Python interpreter that these two are separate character, you should be using raw string (using r before string "")as:
>>> list(r" Hello \t World.")
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']
But here also you'll see two \\
in the resultant list, which is just a Python's way of representing \
.
For Python interpreter '\'
is an invalid string because \'
in a string represent Single quote ('). Hence, when you do '\'
, it raises below error because for Python there is no end quote present in the string:
>>> '\'
File "<stdin>", line 1
'\'
^
SyntaxError: EOL while scanning string literal
If you can't declare your string as raw string (as it's already defined or imported from some other source), you may convert it to byte string by setting encoding as "unicode-escape":
>>> my_str = " Hello \t World."
>>> unicode_escaped_string = my_str.encode('unicode-escape')
>>> unicode_escaped_string
b' Hello \\t World.'
Since it is a byte-string, you need to call chr
to get the corresponding character value of each byte. For example:
>>> list(map(chr, unicode_escaped_string))
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']