Thursday, September 17, 2009

printing options


# python has several options for printing out literals and variables
# the following four print lines all produce the same result

name = "steve"
num = 623
d = {}
d["name"] = name
d["num"] = num
print "Hello " + name + " your number is " + str(num)
print "Hello", name, "your number is", num
print "Hello %s your number is %d" % (name, num)
print "Hello %(name)s your number is %(num)d" %d

output:
Hello steve your number is 623
Hello steve your number is 623
Hello steve your number is 623
Hello steve your number is 623

No comments:

Post a Comment