1

Assume there are two strings:

str1 = 'a\ba'
str2 = 'a'

If we use print to show those strings, all of them will print a.

But obviously, str1 == str2 is False.How could I check whether they are the same after escaping?

Expected output like: f(str1) == str2 is True,f() is the function to get the string after escaping.

I am not looking forward for some arithmetic ways to achieve that.

Kevin Mayo
  • 1,089
  • 6
  • 19
  • Do you want to check whether strings are different after printing? Or at what stage? – NotAName Oct 19 '20 at 02:49
  • @pavel Yes, just want to know whether strings are different after printing. – Kevin Mayo Oct 19 '20 at 02:52
  • Does this answer your question? [Process escape sequences in a string in Python](https://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python) – jkr Oct 19 '20 at 03:23
  • 1
    This is terminal-dependant; see the comments on https://stackoverflow.com/questions/53322564/python-process-escape-characters. – Brian McCutchon Oct 19 '20 at 03:44

1 Answers1

1

\b in ASCII is backspace (BS),it will not show in print function,

1. If you want it show in print function,you can use:

>>> print(repr(str1))
'a\x08a'

2. If you want use this str1 as print,you need to remove backspace :

>>> re.sub('.\b', '', str1)
'a'
Zhubei Federer
  • 1,274
  • 2
  • 10
  • 27