Reputation: 7832
I am using python to communicate with the OS.
I need to create a string of the following form:
string = "done('1') && done('2')"
Note that my string MUST have the double quotes in it, but I am not sure how to do that since the double quotes are used in python for defining a string.
Then I do something like:
os.system(string)
But the system would only read the string with the double and single quotes in it.
I tried:
>>> s = '"done('1') && done('2')"'
File "<stdin>", line 1
s = '"done('1') && done('2')"'
^
SyntaxError: invalid syntax
I also tried the triple quotes suggested here but i get an error:
>>> s = """"done('1') && done('2')""""
File "<stdin>", line 1
s = """"done('1') && done('2')""""
^
SyntaxError: EOL while scanning string literal
Upvotes: 4
Views: 6116
Reputation: 159905
When you use a triply quoted string you need to remember that the string ends when Python finds a closing set of three quotes - and it is not greedy about it. So you can:
Change to wrapping in triple single quotes:
my_command = '''"done('1') && done('2')"'''
Escape the ending quote:
my_command = """"done('1') && done('2')\""""
or add space around your quotes and call strip
on the resulting string:
my_command = """
"done('1') && done('2')"
""".strip()
# Blank lines are for illustrative purposes only
# You can do it all on one line as well (but then it looks like you have
# 4 quotes (which can be confusing)
Upvotes: 5
Reputation: 34
I think this is what you are expecting: string = "\"done('1') && done('2')\""
Ignore if this does not answer your question.
Upvotes: 0
Reputation: 177674
All four flavors of quotes:
print('''"done('1') && done('2')"''') # No escaping required here.
print(""""done('1') && done('2')\"""")
print("\"done('1') && done('2')\"")
print('"done(\'1\') && done(\'2\')"')
Output:
"done('1') && done('2')"
"done('1') && done('2')"
"done('1') && done('2')"
"done('1') && done('2')"
Upvotes: 2
Reputation: 25693
You can escape both kinds of quotes:
s = '"done(\'1\') && done(\'2\')"'
Upvotes: 3