114

Consider:

>>> r"what"ever"
SyntaxError: invalid syntax
>>> r"what\"ever"
'what\\"ever'

So how do we get the quote, but not the slash?

And please don't suggest r'what"ever', because then the question just becomes how do we include both types of quotes?

Related

starball
  • 20,030
  • 7
  • 43
  • 238
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • In the motivating use case for raw strings (regexes and other cases where something other than Python interprets the backslashes), the backslash is fine, because it will be processed by the regex engine/whatever engine. This doesn't apply to all cases where you might want to use raw strings, but make sure to think about whether it applies to your use case before you try to take the backslashes out. – user2357112 Nov 13 '18 at 20:06

7 Answers7

183

If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead:

r'what"ever'

If you need both kinds of quotes in your string, use a triple-quoted string:

r"""what"ev'er"""

If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely case), you can't do it, and you'll have to use non-raw strings with escapes.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 3
    Excellent. Took me a second to realize I could make triple-quoted strings raw too. – mpen Jan 07 '11 at 21:35
  • 3
    I like the last paragraph too... this was exactly what I was trying to figure out. The limitations of different quoting styles. I'm comparing Python's `r"raw string"` and C#'s `@"@-quoted string"` and `"""triple quotes"""` – mpen Jan 07 '11 at 21:41
  • 2
    Python newb here, bit confused, isn't `r'what"ever"` a parse error? Did you mean `r'what"ever'`? – nmr Aug 14 '14 at 23:07
  • 1
    What's wrong with Python developers? Why didn't they simply implemented an escape system like every other languages do? – Jamby Apr 02 '16 at 10:07
  • 7
    @Jamby They do have an escape system, you just use it with non-raw string (strings without the r in front of them), and it works [like pretty much every other language](https://docs.python.org/3.1/reference/lexical_analysis.html#string-and-bytes-literals). – Cobertos Nov 17 '16 at 19:05
  • triple quotes! touché – Kermit Jun 19 '18 at 01:04
  • 1
    Just for fun, here's another kind of text you cannot define with a raw string, even using triple quotes: a string starting with a single quote and ending with a double quote (or vice versa). ;-) – MiniQuark Jul 07 '18 at 07:50
  • 3
    @MiniQuark: `r''''foo"'''` and `r""""foo'"""` work just fine to produce such strings – Adam Rosenfield Jul 21 '18 at 23:41
15

If you need any type of quoting (single, double, and triple for both) you can "combine"(0) the strings:

>>> 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 may 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 joins the strings, and it does not create multiple strings. If you add the "+" operator, then multiple strings are created and combined.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
10

Python has more than one way to do strings. The following string syntax would allow you to use double quotes:

'''what"ever'''
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karl
  • 6,035
  • 5
  • 30
  • 39
5

Nevermind, the answer is raw triple-quoted strings:

r"""what"ever"""
mpen
  • 272,448
  • 266
  • 850
  • 1,236
5

Since I stumbled on this answer, and it greatly helped me, but I found a minor syntactic issue, I felt I should save others possible frustration. The triple quoted string works for this scenario as described, but note that if the " you want in the string occurs 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 hit an error at execution because the """" (4) quotes in a row confuses the string reader, as it thinks it has hit the end of the string already and then finds a random " out there. You can validate this by inserting a space into the 4 quotes like so: " """ and it will not have the error.

In this special case you will need to either use:

somestr = 'This.....at the end"'

or use the method described above of building multiple strings with mixed " and ' and then concatenating them after the fact.

charrold303
  • 83
  • 1
  • 7
2

Just to include new Python f String compatible functionality:

var_a = 10

f"""This is my quoted variable: "{var_a}". """
illusionx
  • 3,021
  • 1
  • 11
  • 17
-3

Use:

dqote='"'
sqote="'"

Use the '+' operator and dqote and squote variables to get what you need.

If I want sed -e s/",u'"/",'"/g -e s/^"u'"/"'"/, you can try the following:

dqote='"'
sqote="'"
cmd1="sed -e s/" + dqote + ",u'" + dqote + "/" + dqote + ",'" + dqote + '/g -e s/^"u' + sqote + dqote + '/' + dqote + sqote + dqote + '/'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zulfi123786
  • 165
  • 1
  • 2
  • 13