Raw data to string convert i.e. r'nwow' to "wow"

Question:

As I was browsing through a regular expression while learning python, I was not sure how to convert the regular expression pattern to normal string.

e.g. 
a='nwow'  ==> print a ==> output: wow
b=repr(a)  ==> print b ==> output: nwow

how to print wow from “b” variable not using “a” variable?

Asked By: atchuthan

||

Answers:

You need to decode the raw string to get the interpreted result:

>>> i = r'nwow'
>>> print(i)
nwow
>>> print(i.decode('string_escape'))

wow
>>>

Note that you will not get just wow, because the string is nwow hence the gap when I print it; also be careful about escaping because the string is now changed:

>>> q = i.decode('string_escape')
>>> len(q) == len(i)
False
>>> q
'nwow'
>>> i
'\nwow'
Answered By: Burhan Khalid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.