Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > why the output is different when i am implementig multiline string

Reply
Thread Tools

why the output is different when i am implementig multiline string

 
 
stringsatif1@gmail.com
Guest
Posts: n/a
 
      01-02-2013
>>> '''hello
world'''
'hello\nworld'
>>> fred=''' hello

world'''
>>> print(fred)

hello
world
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-02-2013
On Thu, Jan 3, 2013 at 1:00 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      01-02-2013
On 01/02/2013 09:00 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
stringsatif1@gmail.com
Guest
Posts: n/a
 
      01-02-2013
thanks dave..
 
Reply With Quote
 
stringsatif1@gmail.com
Guest
Posts: n/a
 
      01-02-2013
thanks dave..
 
Reply With Quote
 
stringsatif1@gmail.com
Guest
Posts: n/a
 
      01-02-2013
thanks cris
 
Reply With Quote
 
stringsatif1@gmail.com
Guest
Posts: n/a
 
      01-02-2013
thanks cris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementig a XCAP web server: Ebb and Rack ? Iñaki Baz Castillo Ruby 2 08-29-2009 02:20 AM
Implementig a XCAP web server: Ebb and Rack ? Iñaki Baz Castillo Ruby 0 08-27-2009 09:27 PM
How to match string end for a multiline string? Peng Yu Perl Misc 2 06-24-2008 02:32 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
how to define a variable to hold a multiline text input in perl from html multiline textbox dale zhang Perl Misc 8 11-30-2004 06:53 AM



Advertisments