newb question on strings

Dan Bishop danb_83 at yahoo.com
Tue Jun 24 23:32:45 EDT 2008


On Jun 24, 4:04 pm, "shand... at gmail.com" <shand... at gmail.com> wrote:
> Are you trying to escape for a regular expression?
>
> Just do re.escape().
>
> >>> print re.escape('Happy')
> Happy
> >>> print re.escape("Frank's Diner")
>
> Frank\'s\ Diner
>
> If you're escaping for URLs, there's urllib2.quote(), for a command
> line, use subprocess.list2cmdline.

And if you're escaping for string literals in Python (or C and its
descendants), you can do:

>>> print "Frank's Diner".encode('string-escape')
Frank\'s Diner




More information about the Python-list mailing list