You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
Copy>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
Copy>>> print(r'baz "\"')
baz "\"
Answer from Charles Duffy on Stack OverflowYou're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
Copy>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
Copy>>> print(r'baz "\"')
baz "\"
Use a raw string:
Copy>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
Note that although it looks wrong, it's actually right. There is only one backslash in the string foo.
This happens because when you just type foo at the prompt, python displays the result of __repr__() on the string. This leads to the following (notice only one backslash and no quotes around the printed string):
Copy>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
And let's keep going because there's more backslash tricks. If you want to have a backslash at the end of the string and use the method above you'll come across a problem:
Copy>>> foo = r'baz \'
File "<stdin>", line 1
foo = r'baz \'
^
SyntaxError: EOL while scanning single-quoted string
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
Copy>>> foo = 'baz \\'
>>> print(foo)
baz \
However, if you're working with Windows file names, you're in for some pain. What you want to do is use forward slashes and the os.path.normpath() function:
Copymyfile = os.path.normpath('c:/folder/subfolder/file.txt')
open(myfile)
This will save a lot of escaping and hair-tearing. This page was handy when going through this a while ago.
regex - In Python, how can you write the string String = "\s"? - Stack Overflow
Backslash error
The use of slash in python string and regex - Stack Overflow
What does a backslash by itself ('\') mean in Python? - Stack Overflow
Videos
I need to make a string that contains "\" but it doesn't work
SyntaxError: unterminated string literal (detected at line 15)
I believe this is related to Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
The representation of the string and what it actually contains can differ.
Observe:
Copy>>> B = "The" + "\s"
>>> B
'The\\s'
>>> print B
The\s
Furthermore
Copy>>> A = "The Cat and Dog"
>>> B = str.replace(A, ' ', '\s')
>>> B
'The\\sCat\\sand\\sDog'
>>> print B
The\sCat\sand\sDog
From the docs:
all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result
So while \s is not a proper escape sequence, Python forgives you your mistake and treats the backslash as if you had properly escaped it as \\. But when you then view the string's representation, it shows the backslash properly escaped. That said, the string only contains one backslash. It's only the representation that shows it as an escape sequence with two.
So it seems that python would convert \sto \s.
Don't confuse string representations with the actual content of the string. String representation is the way you write a string in source code, which may not exactly be the same as the string actually in memory. Backslashes are parsed specially to allow you to write non-printable characters using the backslash syntax. In this case, \s is not a valid escape sequence so the python parser interprets it literally as backslash-s. In memory, the string is still a character sequence containing the letters: `\, s
str class have a __repr__()/repr() method that returns a string that contains the source-code representation of the string, this is the string that gets printed when you don't use print statement in the REPL. This allows you to copy paste those string and reuse it in another part of the shell, but it isn't really what is stored in memory and how python interprets the string. When printing repr, python always escapes a literal backslash, this is to remove ambiguity on whether the backslash is interpreted as escape sequence or as a literal character.
Why would Python do this and what is this for? Is it the same in other languages like Java?
Most languages' string literal do interpret backslash escape sequence, although different languages treats invalid escape sequence differently. In Python, invalid backslash escape sequence is silently treated as literal backslash instead of producing an error. You'd probably encounter this kind of issue more often in Python because it has an ubiquitous repr() protocol and the default use of repr in the REPL shell.
Python is just escaping it, so when it sees an "\" continued by a letter and if that letter doesn't have any special meaning then Python actually escapes the backslash, instead of throwing any errors.
Python interactive interface uses repr to return a string containing a printable representation of an object. So that function is adding the extra backslash to indicate that it's a literal backslash.
If you use print function to show the value of str1, you will get it printed in the stdout with just 1 backslash.
Look at this example:
str1 = '\s'
print str1
print str1.__repr__()
A backslash at the end of a line tells Python to extend the current logical line over across to the next physical line. See the Line Structure section of the Python reference documentation:
2.1.5. Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters (
\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
There is also the option to use implicit line joining, by using parentheses or brackets or curly braces; Python will not end the logical line until it finds the matching closing bracket or brace for each opening bracket or brace. This is the recommended code style, the sample you found should really be written as:
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
words_and_emoticons[i+1].lower() == "of") or
item.lower() in BOOSTER_DICT):
sentiments.append(valence)
continue
See the Python Style Guide (PEP 8) (but note the exception; some Python statements don't support (...) parenthesising so backslashes are acceptable there).
Note that Python is not the only programming language using backslashes for line continuation; bash, C and C++ preprocessor syntax, Falcon, Mathematica and Ruby also use this syntax to extend lines; see Wikipedia.
In this case, the \ is escaping the following new line character. Because Python cares about whitespace, this code is using this to allow code to be continued on a new line.