How to use quotation marks inside quotation marks and exchange word from within argument?

-1

Running the

os.system("reg add "caminho\caminho\caminho\caminho\"")

And if there is more, how can I choose a word in the middle of this command and replace with another one?

    
asked by anonymous 29.06.2018 / 19:46

1 answer

1

To use quotation marks inside a string definition, you need to escape it with \ .

So:

"meu nome é \"wallace\"

The result will be:

'meu nome é "wallace"'

You could also substitute double quotation marks for single quotes to make it easier to read:

'meu nome é "Wallace"'

To replace a specific snippet, use the replace method of your str .

So:

'meu nome é Robin'.replace('Robin', 'Wallace')

The result will be:

'meu nome é Wallace'

My thoughts and recommendations

To execute this command, I would not escape the string, but rather, I would use the formatting of str , to improve readability, like this:

cmd = 'reg add "{0}"'.format("caminho\caminho\caminho")

os.system(cmd)

Or even using the % operator to set str .

caminho = "caminho\caminho\caminho"
cmd = "reg add "%s"' % caminho;
    
29.06.2018 / 22:02