0

I am currently solving Project Euler 59. I take given ASCII sequence and xor it with 3 letter converted ASCII key. When I xor it and 2 elements in a row by chance are \ and n then my result becomes like that:

\\n instead of \n

As far as I understand it puts extra backslash to ignore new line, but I need my list exactly as it is for obvious reason. How can I have \n in my list without it becoming new line?

EDIT: now I double checked, everytime xor result becomes \ , it changes to \\ automatically.

user3763437
  • 359
  • 2
  • 10

2 Answers2

0

I think you misunderstand "\\" is one character:

print len("\\") 
print ord("\\")
msturdy
  • 10,479
  • 11
  • 41
  • 52
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

The '\\n' you are seeing is actually a '\n'. The '\\' is just the way a backslash is escaped in strings, and as Joran points out, is a single character. For more information on escape sequences in Python strings, see https://docs.python.org/2/reference/lexical_analysis.html#string-literals

>>> s = '\\n'
>>> print s
\n
>>> print repr(s)
'\\n'
Blair
  • 6,623
  • 1
  • 36
  • 42
  • So what you mean is if I do calculations or comparisons with element `.\\n` it will be exactly the same as if I did with element `.\n`? – user3763437 Jul 24 '14 at 20:54
  • No. `.\\n` is a period followed by a backslash followed by the letter `n`. `.\n` is a period followed by a newline. The way to have a literal backslash in a string in Python is with `'\\'`. – Blair Jul 24 '14 at 20:58
  • What I meant, if I let's say convert `.\\n` to integer, will it be the same result as if I converted ASCII characters `.\n` to integer? Edit: going to check this myself. – user3763437 Jul 24 '14 at 21:01