Difference between %d, %s, %r operator in Python?




Can you tell me the exact difference between %d, %s and %r. I understand %r is used for debugging but i want to know which is the ideal case where we can use these?

I’ll appreciate if someone can explain these using an example.

Thanks in advance.



These are operators related to string operations in Python.

Typically %d refers to digit (or number) and %s refers to a string. So, you use the operator based on what you are printing

Here is an example:

string_var = "Kunal"
digit_car = 21

print "%s is %d" %(string_var, digit_var) 

P.S. This is for Python 2.7. If you are using Python 3., you will need some format changes