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


Groups > comp.lang.python > #35979 > unrolled thread

why the output is different when i am implementig multiline string

Started bystringsatif1@gmail.com
First post2013-01-02 06:00 -0800
Last post2013-01-02 09:22 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  why the output is different when i am implementig multiline string stringsatif1@gmail.com - 2013-01-02 06:00 -0800
    Re: why the output is different when i am implementig multiline string Chris Angelico <rosuav@gmail.com> - 2013-01-03 01:21 +1100
    Re: why the output is different when i am implementig multiline string Dave Angel <d@davea.name> - 2013-01-02 09:22 -0500

#35979 — why the output is different when i am implementig multiline string

Fromstringsatif1@gmail.com
Date2013-01-02 06:00 -0800
Subjectwhy the output is different when i am implementig multiline string
Message-ID<e7c60393-8d39-40f8-9e7a-800b39169721@googlegroups.com>
>>> '''hello
world'''
'hello\nworld'
>>> fred=''' hello
world'''
>>> print(fred)
 hello
world

[toc] | [next] | [standalone]


#35989

FromChris Angelico <rosuav@gmail.com>
Date2013-01-03 01:21 +1100
Message-ID<mailman.1568.1357136513.29569.python-list@python.org>
In reply to#35979
On Thu, Jan 3, 2013 at 1:00 AM,  <stringsatif1@gmail.com> wrote:
>>>> '''hello
> world'''
> 'hello\nworld'
>>>> fred=''' hello
> world'''
>>>> print(fred)
>  hello
> world

That's because repr() converts the newline into "\n", while print
renders it literally. Check out repr() in the docs:

http://docs.python.org/3/library/functions.html?highlight=repr#repr

ChrisA

[toc] | [prev] | [next] | [standalone]


#35990

FromDave Angel <d@davea.name>
Date2013-01-02 09:22 -0500
Message-ID<mailman.1569.1357136556.29569.python-list@python.org>
In reply to#35979
On 01/02/2013 09:00 AM, stringsatif1@gmail.com wrote:
>>>> '''hello
> world'''
> 'hello\nworld'
>>>> fred=''' hello
> world'''
>>>> print(fred)
>  hello
> world

What you're seeing has nothing to do with the triple quotes, and
everything to do with how you're using the debugger.  In one case, you
just mention a value, and the debugger magically calls repr() on the
expression.  So it adds quotes around it, and turns embedded funny stuff
into escape sequences, because that's what repr() does on a string.

In the second case, you call Python's print function (assuming python 3,
which you didn't specify).  it does not call repr(), but just sends the
characters direct to the console.

if you want to see the escape characters in the second case, you should
have either said:

>>>fred

or

>>>print(repr(fred))



-- 

DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web