convert string to the "escaped string"

P

Petr Jakes

Hi,
I am trying to convert string to the "escaped string".
example: from "0xf" I need "\0xf"
I am able to do it like:

a="0xf"
escaped_a=("\%s" % a ).decode("string_escape")

But it looks a little bit complicated in this beautiful language to me
.....

Regards

Petr Jakes
 
P

Petr Jakes

small mistake in my previous posting, sorry!!!!
from "0xf" I need "\xf" of course
and mentioned code gives me "\0xf" of course.

Thanks for your postings

Petr Jakes
 
F

Fredrik Lundh

Petr said:
I am trying to convert string to the "escaped string".
example: from "0xf" I need "\0xf"
I am able to do it like:

a="0xf"
escaped_a=("\%s" % a ).decode("string_escape")

But it looks a little bit complicated in this beautiful language to me

I suspect that optimizing for people who want to turn "0xf" into
chr(0) + "xf" isn't exactly a priority...

if that's really what you want, of course. are you sure you didn't
mean to write, say

chr(int("0xf", 0))

(that is, convert 0xf to a character) or

repr(chr(int("0xf", 0)))

(that is, convert 0xf to a character, and convert that character to a
string literal)

?

</F>
 
P

Petr Jakes

Fredrik, thanks for your posting. I really appretiate your reply and
your way how you kindly answer even stupid newbie's questions on this
mailing list.
I was trying: int("0xf") but I did not have idea about the optional
"base" parameter for the int function.
Thanks a lot.
Petr
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top