My file a
contains the text,
bcd\\\\.
With bash, i read the file and print characters from 4th to 8th position as,
tmp=$(cat a)
echo "${tmp:3:4}"
It prints,
\\\\
All happy. Now i use python's array slicing to print characters from 4th to 8th position as,
>>> f = open('a')
>>> v=f.read()
>>> v[3:7]
It prints,
'\\\\\\\\'
Why does bash and python behave differently when there are backslashes?
Best Answer
It is a matter of how python displays strings. Observe:
When displaying
v[3:7]
, the backslashes are escaped. When printing,print v[3:7]
, they are not escaped.Other examples
The line in your file should end with a newline character. In that case, observe:
The newline character is displayed as a backslash-n. It prints as a newline.
The results for tab are similar: