221k views
1 vote
What's the point of %s and %d when you can just use %r?

a. The %r is best for debugging, and the other formats are for actually displaying variables to users.
b. %s is specifically for strings, %d is for integers, and %r is a generic format for any type.
c. %r is deprecated; %s and %d are the preferred formats for all variable types.
d. %r is for rounding, %s is for strings, and %d is for decimal numbers.

1 Answer

4 votes

Final answer:

The %s and %d formatting symbols are for strings and integers, respectively, while %r is mainly used for debugging by providing a 'raw' string representation of any type.

Step-by-step explanation:

The different formatting symbols, %s, %d, and %r, serve specific purposes in Python string formatting. The symbol %s is used for strings, formatting whatever value you give it as a string. The symbol %d is used for integers, converting the value to a decimal representation. On the other hand, %r uses the repr() method to create a string representation of the value that includes quotations and escape characters - this is typically used for debugging purposes because it gives the 'raw' format of the value. As a result, option (a) is correct: %r is best for debugging, while %s and %d are used for displaying formatted variables to users.

User PeterJames
by
8.3k points