Why do they use %d instead of %s?


#1



Building Useful Classes


class Point3D(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
    
    def __repr__(self):
        return "(%d, %d, %d)" % (self.x, self.y, self.z)
        
my_point = Point3D(1, 2, 3)

print my_point

Why do they use %d here instead of what they have been showing us? (%s)


#2

You can use any letter. You can even use multiple! But be careful with that.


#3

Why would they do that? Wouldn't it make more sense to do something like "%%"? I know you didn't write python, lol, but do you know the reason for this?


#4

One of them %s is for formatting strings. The other, %d is for integers.

s stands for string and d stands for decimal.

eg.

"My name is %s and I'm %d" % ('Mark', 24) #My name is Mark and I'm 24

Edit: I lied about using any letter haha. Use %s and %d...and other ones you may come across.


#5

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