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?
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'
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?
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'