Print a list to a string?

Discussion in 'Python' started by mrstephengross, Oct 31, 2007.

  1. I would like to get the results of a print operation placed in a
    string. For instance, you can very easily create a list and print it
    to stdout:

    x = [1,2,3]
    print x # Will print [1,2,3]

    What if I want the text "[1,2,3]" placed in a string? For instance,
    something like:

    x = [1,2,3]
    str = ''
    print str x # x = '[1,2,3]'

    Any ideas?

    Thanks,
    --Steve
     
    mrstephengross, Oct 31, 2007
    #1
    1. Advertising

  2. mrstephengross

    Gary Herron Guest

    mrstephengross wrote:
    > I would like to get the results of a print operation placed in a
    > string. For instance, you can very easily create a list and print it
    > to stdout:
    >
    > x = [1,2,3]
    > print x # Will print [1,2,3]
    >
    > What if I want the text "[1,2,3]" placed in a string? For instance,
    > something like:
    >
    > x = [1,2,3]
    > str = ''
    > print str x # x = '[1,2,3]'
    >
    > Any ideas?
    >
    > Thanks,
    > --Steve
    >
    >

    Use str(any-object) or repr(any_object) to turn an object into a string
    representation of that object. Use str for a human friendlier
    representation, and repr for a more explicit representation.

    Gary Herron
     
    Gary Herron, Oct 31, 2007
    #2
    1. Advertising

  3. mrstephengross a écrit :
    > I would like to get the results of a print operation


    print is a statement, it doesn't yield any 'result'.

    > placed in a
    > string. For instance, you can very easily create a list and print it
    > to stdout:
    >
    > x = [1,2,3]
    > print x # Will print [1,2,3]
    >
    > What if I want the text "[1,2,3]" placed in a string? For instance,
    > something like:
    >
    > x = [1,2,3]
    > str = ''


    You're shadowing the builtin str type here.

    > print str x # x = '[1,2,3]'
    >
    > Any ideas?


    any of the following should do:

    s = "%s" % x
    s = repr(x)
     
    Bruno Desthuilliers, Oct 31, 2007
    #3
  4. On Wed, 31 Oct 2007 22:17:48 +0000, mrstephengross wrote:

    > I would like to get the results of a print operation placed in a string.


    s = str(x)


    If you specifically need to capture the output of print, then something
    like this:


    >>> import cStringIO
    >>> s = cStringIO.StringIO()
    >>> print >>s, [1, 2, 1.0/5, 'hello world']
    >>> s.getvalue()

    "[1, 2, 0.20000000000000001, 'hello world']\n"


    (And please, please, PLEASE don't ask why 1/5 is 0.2000...01 until you've
    read the FAQs on the Python website. Thank you.)



    --
    Steven
     
    Steven D'Aprano, Oct 31, 2007
    #4
  5. > >>> import cStringIO
    > >>> s = cStringIO.StringIO()
    > >>> print >>s, [1, 2, 1.0/5, 'hello world']
    > >>> s.getvalue()


    Thanks--this works perfectly!

    -_Steve
     
    mrstephengross, Nov 1, 2007
    #5
    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. keto
    Replies:
    0
    Views:
    980
  2. ssecorp
    Replies:
    47
    Views:
    1,066
    Default User
    Aug 8, 2008
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    366
    David Cournapeau
    Dec 30, 2008
  4. Mirko Dziadzka

    print string as raw string

    Mirko Dziadzka, Feb 17, 2009, in forum: Python
    Replies:
    5
    Views:
    389
    Miles
    Feb 17, 2009
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    198
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page