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
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)
You can escape both kinds of quotes:
s = '"done(\'1\') && done(\'2\')"'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With