Just wanted to emphasize what @metatoaster said in the comment above, since I skipped it first.
Using repr (string) will add single quotes, then double quotes outside of it, then single quotes outside of it with escaped internal single quotes, and then to another escaping.
Using repr () as an inline function is more direct, unless there are other conflicts.
s = 'strOrVar' print s, repr(s), repr(repr(s)), ' ', repr(repr(repr(s))), repr(repr(repr(repr(s)))) # prints: strOrVar 'strOrVar' "'strOrVar'" '"\'strOrVar\'"' '\'"\\\'strOrVar\\\'"\''
docs states that its main expression is state (), i.e. the view is the reverse of eval ():
"For many types, this function tries to return a string that will give an object with the same value when passed to eval (), ..."
Backquotes will be shorter but removed in Python 3+. Interestingly, StackOverflow uses backquotes to specify code spaces, instead of highlighting a block of code and pressing a code button - it has an interesting behavior.
aazxcqwe
source share