print() a list

Discussion in 'Python' started by DarkBlue, Sep 5, 2009.

  1. DarkBlue

    DarkBlue Guest

    I am trying to get used to the new print() syntax prior to installing
    python 3.1:

    test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
    ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
    ["ZM", "Zambia"],["ZW", "Zimbabwe"],]

    #old print

    for z in test:
    if z[0].startswith('W'):
    print z[0] , z[1]

    print


    # new print()
    # now a list would have to be printed like this to be equal to old
    print ?

    for z in test:
    if z[0].startswith('W'):
    print('%s %s') % (z[0] , z[1])

    print

    # this output prints the brackets etc. too, not what we want

    for z in test:
    if z[0].startswith('W'):
    print(z[0] , z[1])

    print



    on python 2.6 I get following output:

    WF Wallis and Futuna

    WF Wallis and Futuna

    ('WF', 'Wallis and Futuna')


    Before actually installing python 3.1 my question is if the py2to3
    converter also considers this situation ?

    Thanks
    Db
    DarkBlue, Sep 5, 2009
    #1
    1. Advertising

  2. DarkBlue

    Mark Tolonen Guest

    "DarkBlue" <> wrote in message
    news:...
    >I am trying to get used to the new print() syntax prior to installing
    > python 3.1:
    >
    > test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
    > ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
    > ["ZM", "Zambia"],["ZW", "Zimbabwe"],]
    >
    > #old print
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print z[0] , z[1]
    >
    > print
    >
    >
    > # new print()
    > # now a list would have to be printed like this to be equal to old
    > print ?
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print('%s %s') % (z[0] , z[1])
    >
    > print
    >
    > # this output prints the brackets etc. too, not what we want
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print(z[0] , z[1])
    >
    > print
    >
    >
    >
    > on python 2.6 I get following output:
    >
    > WF Wallis and Futuna
    >
    > WF Wallis and Futuna
    >
    > ('WF', 'Wallis and Futuna')
    >
    >
    > Before actually installing python 3.1 my question is if the py2to3
    > converter also considers this situation ?


    Without the following statement, print does not work the "new" way. What
    you are printing is a tuple of the two list elements.

    from __future__ import print_function

    test = [
    ["VG", "Virgin Islands, British"],
    ["VI", "Virgin Islands, U.S."],
    ["WF", "Wallis and Futuna"],
    ["EH", "Western Sahara"],
    ["YE", "Yemen"],
    ["ZM", "Zambia"],
    ["ZW", "Zimbabwe"]]

    for z in test:
    if z[0].startswith('Z'):
    print(z[0],z[1])
    print()

    ----results----
    ZM Zambia
    ZW Zimbabwe

    Comment out "from __future__ import print_function" and you'll get:
    Mark Tolonen, Sep 5, 2009
    #2
    1. Advertising

  3. DarkBlue

    Mark Tolonen Guest

    "DarkBlue" <> wrote in message
    news:...
    >I am trying to get used to the new print() syntax prior to installing
    > python 3.1:
    >
    > test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
    > ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
    > ["ZM", "Zambia"],["ZW", "Zimbabwe"],]
    >
    > #old print
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print z[0] , z[1]
    >
    > print
    >
    >
    > # new print()
    > # now a list would have to be printed like this to be equal to old
    > print ?
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print('%s %s') % (z[0] , z[1])
    >
    > print
    >
    > # this output prints the brackets etc. too, not what we want
    >
    > for z in test:
    > if z[0].startswith('W'):
    > print(z[0] , z[1])
    >
    > print
    >
    >
    >
    > on python 2.6 I get following output:
    >
    > WF Wallis and Futuna
    >
    > WF Wallis and Futuna
    >
    > ('WF', 'Wallis and Futuna')
    >
    >
    > Before actually installing python 3.1 my question is if the py2to3
    > converter also considers this situation ?


    You need the following statement to use print() in Python 2.6:

    from __future__ import print_function

    test = [
    ["VG", "Virgin Islands, British"],
    ["VI", "Virgin Islands, U.S."],
    ["WF", "Wallis and Futuna"],
    ["EH", "Western Sahara"],
    ["YE", "Yemen"],
    ["ZM", "Zambia"],
    ["ZW", "Zimbabwe"]]

    for z in test:
    if z[0].startswith('Z'):
    print(z[0],z[1])
    print()

    ----result----
    ZM Zambia
    ZW Zimbabwe

    -Mark
    Mark Tolonen, Sep 5, 2009
    #3
  4. DarkBlue

    Dero Guest

    Re: print() a list

    On Sep 5, 2:35 pm, "Mark Tolonen" <> wrote:
    > "DarkBlue" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >I am trying to get used to the new print() syntax prior to installing
    > > python 3.1:

    >
    > > test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
    > > ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
    > > ["ZM", "Zambia"],["ZW", "Zimbabwe"],]

    >
    > > #old print

    >
    > > for z in test:
    > >  if z[0].startswith('W'):
    > >     print z[0] ,  z[1]

    >
    > > print

    >
    > > # new print()
    > > # now a list would have to be printed like this to be equal to old
    > > print ?

    >
    > > for z in test:
    > >  if z[0].startswith('W'):
    > >     print('%s %s') % (z[0] ,  z[1])

    >
    > > print

    >
    > > # this output prints the brackets etc. too, not what we want

    >
    > > for z in test:
    > > if z[0].startswith('W'):
    > >    print(z[0] , z[1])

    >
    > > print

    >
    > > on python 2.6 I get following output:

    >
    > > WF Wallis and Futuna

    >
    > > WF Wallis and Futuna

    >
    > > ('WF', 'Wallis and Futuna')

    >
    > > Before actually installing python 3.1 my question is if the py2to3
    > > converter also considers this situation ?

    >
    > Without the following statement, print does not work the "new" way.  What
    > you are printing is a tuple of the two list elements.
    >
    > from __future__ import print_function
    >
    > test = [
    >     ["VG", "Virgin Islands, British"],
    >     ["VI", "Virgin Islands, U.S."],
    >     ["WF", "Wallis and Futuna"],
    >     ["EH", "Western Sahara"],
    >     ["YE", "Yemen"],
    >     ["ZM", "Zambia"],
    >     ["ZW", "Zimbabwe"]]
    >
    > for z in test:
    >   if z[0].startswith('Z'):
    >      print(z[0],z[1])
    > print()
    >
    > ----results----
    > ZM Zambia
    > ZW Zimbabwe
    >
    > Comment out "from __future__ import print_function" and you'll get:


    Thank you.

    I thought in 2.6 both print and print() were equally implemented
    without the future import requirement.
    Dero, Sep 5, 2009
    #4
  5. DarkBlue

    AggieDan04 Guest

    Re: print() a list

    On Sep 5, 1:51 am, Dero <> wrote:
    > On Sep 5, 2:35 pm, "Mark Tolonen" <> wrote:
    >
    > > "DarkBlue" <> wrote in message

    >
    > >news:...

    >
    > > >I am trying to get used to the new print() syntax prior to installing
    > > > python 3.1:

    ....
    > > Without the following statement, print does not work the "new" way.  What
    > > you are printing is a tuple of the two list elements.

    >
    > > from __future__ import print_function

    ....
    > I thought in 2.6 both print and print() were equally implemented
    > without the future import requirement.


    That couldn't be done because the print() syntax can't be
    distinguished from old-style print with a tuple.

    ~$ python2.6 -c "print(1, 2)"
    (1, 2)
    ~$ python3.0 -c "print(1, 2)"
    1 2
    AggieDan04, Sep 5, 2009
    #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. Bad_Kid
    Replies:
    3
    Views:
    3,944
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,946
    Kevin Spencer
    Apr 20, 2005
  3. keto
    Replies:
    0
    Views:
    930
  4. David Cournapeau

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

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    348
    David Cournapeau
    Dec 30, 2008
  5. Xavier Noria

    print LIST vs print join "", LIST

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

Share This Page