How do I write a string literal that includes quotation marks?

Question:

I want to create a string with this exact text: nuke.execute("Write1", 1, 10, 1)

Simply surrounding it with double quotes, like "nuke.execute("Write1", 1, 10, 1)", doesn’t work:

>>> "nuke.execute("Write1", 1, 10, 1)"
  File "<stdin>", line 1
    "nuke.execute("Write1", 1, 10, 1)"
                   ^
SyntaxError: invalid syntax
>>> 

How can I write the string literal in my code?

Asked By: jonathan topf

||

Answers:

Simply enclose it in single quotes:

'nuke.execute("Write1", 1, 10, 1)'

There are several alternatives, such as escaping the embedded quotes with backslashes:

"nuke.execute("Write1", 1, 10, 1)"

or using triple-quoted strings:

"""nuke.execute("Write1", 1, 10, 1)"""

or

'''nuke.execute("Write1", 1, 10, 1)'''

You can read more about Python string literals in the manual.

Answered By: NPE

You can use single quotes:

'nuke.execute("Write1", 1, 10, 1)'

or you can use backslashes to “escape” the embedded quotes:

"nuke.execute("Write1", 1, 10, 1)"
Answered By: unwind

You can escape the quotation marks using :

"nuke.execute("Write1", 1, 10, 1)'"
Answered By: kfirbreger

What’s wrong with 'nuke.execute("Write1", 1, 10, 1)'?

Answered By: Noufal Ibrahim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.