How to include a quote in a raw Python string
Consider:
>>> r"what"ever" SyntaxError: invalid syntax >>> r"what\"ever" 'what\\"ever' So, how can we get a quote, not a slash?
And please do not suggest r'what"ever' , because then the question arises: how to include both types of quotes?
Connected
If you want to use double quotes in strings, but not single quotes, you can simply use single quotes instead of a separator:
r'what"ever' If you need both types of quotes in your string, use a string with three quotes:
r"""what"ev'er""" If you want to include strings of both types with three quotes in the string (an extremely improbable case), you will not be able to do this, and you will have to use uneven lines with screens.
Python has several ways to create strings. The following line syntax allows you to use double quotes:
'''what"ever''' If you need any type of quotation (single, double and triple for both), you can "combine" (0) lines:
>>> raw_string_with_quotes = r'double"' r"single'" r'''double triple""" ''' r"""single triple''' """ >>> print raw_string_with_quotes double"single'double triple""" single triple''' You can also "combine" (0) raw strings with non-raw strings:
>>> r'raw_string\n' 'non-raw string\n' 'raw_string\\nnon-raw string\n' (0): In fact, the Python parser concatenates strings and does not create multiple strings. If you add the "+" operator, then several lines will be created and merged.
Nevermind, the answer is raw triple quotes:
r"""what"ever""" Since I stumbled upon this answer, and it helped me a lot, but I found a little syntax problem, I felt that I had to save others from possible disappointment. A string with triple quotes works for this scenario, as described, but note that if "you want in a string, appears at the end of the string itself:
somestr = """This is a string with a special need to have a " in it at the end"""" You will get an error during execution because the quotation marks "" "" (4) in the line confuse the reader of the line, because he thinks that he has already reached the end of the line and then finds a random "there. You can check this by inserting a space in 4 quotation marks are something like this: "" "" and it will not have an error.
In this special case, you will need to use:
somestr = 'This.....at the end"' or use the method described above to build multiple lines with mixed "and" and then concatenate them in fact.
Using:
dqote='"' sqote="'" Use the + operator and the dqote and squote to get what you need.
If I want to use sed -es/",u'"/",'"/g -es/^"u'"/"'"/ , you can try the following:
dqote='"' sqote="'" cmd1="sed -es/" + dqote + ",u'" + dqote + "/" + dqote + ",'" + dqote + '/g -es/^"u' + sqote + dqote + '/' + dqote + sqote + dqote + '/'