The way I use is based on the observation that C ++ strings basically obey the same rules regarding characteristics and escaped as a Javascript / JSON string.
Python, since version 2.6 has a built-in JSON library that can serialize Python data into JSON. Therefore, the code, assuming that we do not need a quote, namely:
import json
string_for_printing = json.dumps(original_string).strip('"')
Krizz source
share