how do I extract a input string literal containing escape sequences like \a,\16 as a raw string?

138 views Asked by At

I want to extract the following input text as raw string but for both r''+ string, repr(string) \a,\1 are getting replaced with \x07,\x01. Will appreciate some help

input_txt = 'ab\a\1'
t1=r''+input_txt
t2=repr(input_txt)
print(t1)
print(t2)
2

There are 2 answers

9
Omid Soroush On

Raw string is created by prefixing a string literal with ‘r’ or ‘R’.

input_txt = r'ab\a\1'
t1 = input_txt
t2 = repr(input_txt)
print(t1)
print(t2)
1
Sakib On

In the Python Raw string, we usually write the word within the quotes, and in addition to it, we add the literal ‘r’ as the prefix to it, and then we assign it to a variable and print that variable. All the Raw strings must contain the literal ‘r’ at its prefix.