repr( open('/etc/motd', 'rt').read() )

Terry Reedy tjreedy at udel.edu
Mon Feb 15 15:03:21 EST 2016


On 2/15/2016 8:05 AM, Veek. M wrote:
> When I do at the interpreter prompt,
> repr( open('/etc/motd', 'rt').read() )
> i get # 1 #:

When posting  questions here or at Stackoverflow or elsewhere, it is a 
really good idea to develop and post a 'minimal, complete, verifiable 
example' that demonstrates the behavior in question.  In this case, the 
open and read calls are just noise.  A string with a newline illustrates 
your question without distraction.

 >>> s = '\n'
 >>> len(s)
1
 >>> len(str(s))
1
 >>> len(repr(s))
4
 >>> s
'\n'
 >>> str(s)
'\n'
 >>> repr(s)
"'\\n'"
 >>> print(s)


 >>> print(str(s))


 >>> print(repr(s))
'\n'
 >>>

For this question, 'at the interpreter prompt' is essential, so leaving 
the >>> prompt is a good idea.  I did the above with 3.5.1 also in IDLE 
and got exactly the same result, which should be the case.

print('start')
s='\n'
print(s)
print(str(s))
print(repr(s))
print('add repr')
print(repr(s))
print(repr(str(s)))
print(repr(repr(s)))
print('end')

duplicates the collective >>> responses seen above and demonstrates, as 
Random832 said, that '>>> expr' prints repr(expr).

start




'\n'
add repr
'\n'
'\n'
"'\\n'"
end

-- 
Terry Jan Reedy



More information about the Python-list mailing list