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
How to store a string containing both single quote( ' ) and double quote( " ) in python
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)
你可以逃避两种报价:
s = '"done(\'1\') && done(\'2\')"'
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')"
I think this is what you are expecting: string = "\\"done('1') && done('2')\\""
Ignore if this does not answer your question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.