What is the difference between %d , %s and {}


#1

I was wondering if all three are placeholders, when do you use each? I think %d is used for numbers and %s is used for strings and {} is used for ?


#2

Excellent question! %s is used for strings whereas %d is used for integers. {} is the new way of formatting in Python. For example in the past you may have wanted to print "Hi, my name is John Doe!" but have the first and last name be a variable. The code would look like this.

print "Hi, my name is %s %s!" % (firstName, lastName)

The new way of formatting uses .format() and the code would look like:

print "Hi, my name is {} {}!".format(firstName, lastName)

#3

Thank you so much , i was wondering why they seemed so similar. I am definitely for the new way of formating :+1:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.