Simple Problems: Caps and Commas

Ron Adam radam2 at tampabay.rr.com
Mon Oct 27 13:19:47 EST 2003


On Sun, 26 Oct 2003 23:24:46 -0600, "Kyle E" <Kyle_E2006 at hotmail.com>
wrote:

>I tried this:
>-----------------------------------------------
>print "Name: ", fname, mname, lname
>print "Address: ",saddy, ",",city, ",",state, ",",zip
>-----------------------------------------------
>
>But I get extra spaces after each value:
>"Name: Firstname , Middlename , Lastname"
>"Address: Street Address , City , State , Zip"
>
>When it should look like this:
>"Name: Firstname, Middlename, Lastname"
>"Address: Street Address, City, State, Zip"
>


Hi, I'm new at  Python also.  This is the what I've found. 


saddy='Home'
city='Chair'
state='Happy'
zip='Computer'

# space inserted
print "Address:",saddy,",",city,",",state,",",zip

# no spaces,+ can't be used at beginning or end of line
print "Address:"+saddy+","+city+","+state+","+zip

# space inserted between two prints
print "Address:"+saddy+","+city+",",
print state+","+zip

# same as above
print "Address:%s,%s," % (saddy,city),
print "%s,%s" % (state,zip)

# does not append '\n' character to end
import sys
sys.stdout.write("Address:%s,%s," % (saddy,city))
sys.stdout.write("%s,%s" % (state,zip))

"""
Results in the folling outputs:

Address: Home , Chair , Happy , Computer
Address:Home,Chair,Happy,Computer
Address:Home,Chair, Happy,Computer
Address:Home,Chair, Happy,Computer
Address:Home,Chair,Happy,Computer
"""

The last one give you the most control.  






More information about the Python-list mailing list