Q:

Python3 replace automaticaly single quote by \'

I am doing a python3 software that use google_speech and and os.system command.  
Everything work fine but when a user enter a string with the ' character I have this error :  Syntax error: Unterminated quoted string  
This is my code :
def textToSpeak():
global fieldValues

msg = "Enter the text to speak\n\nDon't use" +str(" \' ")+str(" write it like this : je tinvite chez moi, not je t\'invite chez moi ")
title = "Enter the text to speak"
fieldNames = ["Text to speak"]
fieldValues = []
fieldValues = multenterbox(msg, title, fieldNames)
speak()

def speak():
global lang, fieldValues
textValue = "google_speech -l" +str(lang) +str(" \'\"")+str(fieldValues[0])+str("\"\'")
os.system(textValue)

A:

If you insist on os.system, you want shlex.quote:

Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.

That said, I'd strongly recommend moving to the subprocess module (subprocess.call would be the simplest replacement for os.system here, though there are other options), and passing your arguments in list form, allowing subprocess to do the work of escaping (when necessary on Windows), removing the need to manually add quotes, and avoiding string processing entirely on other OSes (where it can exec an argument vector directly, with no escapes needed).

