How to print a list

This is the place for queries that don't fit in any of the other categories.

How to print a list

Postby hadjisra » Mon May 27, 2013 2:23 am

This is a very beginer question but im sturggeling trying to find out how to do it. I want to print a list as a modulo, it makes more sense in code.
Code: Select all
fruits = ['apples','oranges','pears']
print "Some examples of fruits are %s, %s, and %s" % fruits


please I could use some help
Thank you
hadjisra
 
Posts: 1
Joined: Mon May 27, 2013 2:19 am

Re: How to print a list

Postby metulburr » Mon May 27, 2013 3:01 am

Code: Select all
fruits = ['apples','oranges','pears']
print "Some examples of fruits are %s, %s, and %s" % (fruits[0], fruits[1], fruits[2])


The newer format method is prefered:
Code: Select all
print("Some examples of fruits are {}, {}, and {}".format(fruits[0], fruits[1], fruits[2]))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1451
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to print a list

Postby DrakeMagi » Mon May 27, 2013 3:32 am

Here some lazy ways
Code: Select all
fruits = ['apples','oranges','pears']
print "Some examples of fruits are %s, %s, and %s" % tuple(fruits)

or
Code: Select all
fruits = ['apples','oranges','pears']
print "Some examples of fruits are {}, {}, and {}".format(*fruits)
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 109
Joined: Sun May 12, 2013 8:36 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest