0

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?

atchuthan
  • 181
  • 4
  • 15

1 Answers1

3

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'
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284