Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   why the output is different when i am implementig multiline string (http://www.velocityreviews.com/forums/t956061-why-the-output-is-different-when-i-am-implementig-multiline-string.html)

stringsatif1@gmail.com 01-02-2013 02:00 PM

why the output is different when i am implementig multiline string
 
>>> '''hello
world'''
'hello\nworld'
>>> fred=''' hello

world'''
>>> print(fred)

hello
world

Chris Angelico 01-02-2013 02:21 PM

Re: why the output is different when i am implementig multiline string
 
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/fun...ight=repr#repr

ChrisA

Dave Angel 01-02-2013 02:22 PM

Re: why the output is different when i am implementig multiline string
 
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


stringsatif1@gmail.com 01-02-2013 03:10 PM

Re: why the output is different when i am implementig multiline string
 
thanks dave..

stringsatif1@gmail.com 01-02-2013 03:10 PM

Re: why the output is different when i am implementig multiline string
 
thanks dave..

stringsatif1@gmail.com 01-02-2013 03:11 PM

Re: why the output is different when i am implementig multiline string
 
thanks cris

stringsatif1@gmail.com 01-02-2013 03:11 PM

Re: why the output is different when i am implementig multiline string
 
thanks cris


All times are GMT. The time now is 07:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.