When would it be preferable to use:
for n in range(0, 100):
print "The count is "+str(n)
Vs.
for n in range(0, 100):
print "The count is %d" % (n)
Or visa versa?
When would it be preferable to use:
for n in range(0, 100):
print "The count is "+str(n)
Vs.
for n in range(0, 100):
print "The count is %d" % (n)
Or visa versa?
The %d
syntax has been deprecated. Use str.format
for string formatting.
Examples:
"My name is {0}.".format("Austin")
"I am {} years old and make ${:.2f} per hour.".format(50, 50.29999) # positional arguments
myDict = { "language" : "English", "major" : "Computer Networking" }
"I speak {language} and have a degree in {major}.".format(**myDict)
myList = [2, "German Sheppard", "Lucy", "Ethel" ]
"I own {} {}s named '{}' and '{}'.".format(*myList)
Read more about str.format
here.