0

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?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
RancheroBeans
  • 500
  • 1
  • 6
  • 15

1 Answers1

1

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.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Goodies
  • 4,439
  • 3
  • 31
  • 57