what's the meaning of
%r in the following statement?
print '%r' % (1)
I think I've heard of
%f but never heard of this.
In Python, there are two builtin functions for turning an object into a string:
>>> print repr('hi') 'hi' # notice the quotes here as opposed to... >>> print str('hi') hi
If returning an expression doesn't make sense for an object,
To answer your original question:
See String Formatting Operations in the docs. Notice that %s and %d etc, might work differently to how you expect if you are used to the way they work in another language such as C.
In particular, %s also works well for ints and floats unless you have special formatting requirements where %d or %f will give you more control.
Adding to the replies given above, '%r' can be useful in a scenario where you have a list with heterogeneous data type. Let's say, we have a list = [1, 'apple' , 2 , 'r','banana'] Obviously in this case using '%d' or '%s' would cause an error. Instead, we can use '%r' to print all these values.