why the output is different when i am implementig multiline string

Discussion in 'Python' started by stringsatif1@gmail.com, Jan 2, 2013.

  1. Guest

    >>> '''hello
    world'''
    'hello\nworld'
    >>> fred=''' hello

    world'''
    >>> print(fred)

    hello
    world
    , Jan 2, 2013
    #1
    1. Advertising

  2. On Thu, Jan 3, 2013 at 1:00 AM, <> 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
    Chris Angelico, Jan 2, 2013
    #2
    1. Advertising

  3. Dave Angel Guest

    On 01/02/2013 09:00 AM, 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
    Dave Angel, Jan 2, 2013
    #3
  4. Guest

    thanks dave..
    , Jan 2, 2013
    #4
  5. Guest

    thanks dave..
    , Jan 2, 2013
    #5
  6. Guest

    thanks cris
    , Jan 2, 2013
    #6
  7. Guest

    thanks cris
    , Jan 2, 2013
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  3. Iñaki Baz Castillo

    Implementig a XCAP web server: Ebb and Rack ?

    Iñaki Baz Castillo, Aug 27, 2009, in forum: Ruby
    Replies:
    0
    Views:
    272
    Iñaki Baz Castillo
    Aug 27, 2009
  4. Iñaki Baz Castillo

    Implementig a XCAP web server: Ebb and Rack ?

    Iñaki Baz Castillo, Aug 27, 2009, in forum: Ruby
    Replies:
    2
    Views:
    326
    David Masover
    Aug 29, 2009
  5. dale zhang
    Replies:
    8
    Views:
    420
    Tintin
    Nov 30, 2004
Loading...

Share This Page