Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102973

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

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: repr( open('/etc/motd', 'rt').read() )
Date 2016-02-15 15:03 -0500
Message-ID <mailman.147.1455566619.22075.python-list@python.org> (permalink)
References <n9sie9$91s$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

repr( open('/etc/motd', 'rt').read() ) "Veek. M" <vek.m1234@gmail.com> - 2016-02-15 18:35 +0530
  Re: repr( open('/etc/motd', 'rt').read() ) Random832 <random832@fastmail.com> - 2016-02-15 09:47 -0500
  Re: repr( open('/etc/motd', 'rt').read() ) Terry Reedy <tjreedy@udel.edu> - 2016-02-15 15:03 -0500
  Re: repr( open('/etc/motd', 'rt').read() ) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-16 17:43 +1100

csiph-web