print() a list

D

DarkBlue

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
 
M

Mark Tolonen

DarkBlue said:
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:
 
M

Mark Tolonen

DarkBlue said:
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
 
D

Dero

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]

# 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])

# 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])

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.
 
A

AggieDan04

....
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top