Replacing backslashes in Python strings

Posted by user323659 on Stack Overflow See other posts from Stack Overflow or by user323659
Published on 2010-05-20T23:15:40Z Indexed on 2010/05/20 23:20 UTC
Read the original article Hit count: 175

I have some code to encrypt some strings in Python. Encrypted text is used as a parameter in some urls, but after encrypting, there comes backslashes in string and I cannot use single backslash in urllib2.urlopen.

I cannot replace single backslash with double. For example:

print cipherText 

'\t3-@\xab7+\xc7\x93H\xdc\xd1\x13G\xe1\xfb'

print cipherText.replace('\\','\\\\')

'\t3-@\xab7+\xc7\x93H\xdc\xd1\x13G\xe1\xfb'

Also putting r in front of \ in replace statement did not worked.

All I want to do is calling that kind of url:

http://awebsite.me/main?param="\t3-@\xab7+\xc7\x93H\xdc\xd1\x13G\xe1\xfb"

And also this url can be successfully called:

http://awebsite.me/main?param="\\t3-@\\xab7+\\xc7\\x93H\\xdc\\xd1\\x13G\\xe1\\xfb"

Any idea will be appreciated.

© Stack Overflow or respective owner

Related posts about python

Related posts about backslash