Create a backslash-escaped version of a string?

B

boblatest

Hello,

I'd like to have control characters in a string to be converted to
their backslash-escaped counterparts. I looked in the encoders section
of the string module but couldn't find anything appropriate. I could
write it myself but I'm sure something of the sort exists. The
hypothetical method "c_escaped()" would work like this:

Thanks,
robert
 
C

Chris Rebert

Hello,

I'd like to have control characters in a string to be converted to
their backslash-escaped counterparts. I looked in the encoders section
of the string module but couldn't find anything appropriate. I could
write it myself but I'm sure something of the sort exists. The
hypothetical method "c_escaped()" would work like this:

abc\rdef

print a.encode("string-escape")

Note that there are some wrinkles if the string contains quote marks
(and possibly also if it contains Unicode; I didn't test).

Cheers,
Chris
 
B

boblatest

print a.encode("string-escape")

How could I miss that? I was on that doc page already. Should have
typed "/escape" in the browser ;-)

Thanks,
robert
 
A

Aahz

I'd like to have control characters in a string to be converted to
their backslash-escaped counterparts. I looked in the encoders section
of the string module but couldn't find anything appropriate. I could
write it myself but I'm sure something of the sort exists. The
hypothetical method "c_escaped()" would work like this:

This is a little different from what you want, but just for the sake of
completeness:
abc\rdef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top