These are all informative answers, but none are quite getting at the core of what the difference is between %s
and %d
.
%s
tells the formatter to call the str[]
function on the argument and since we are coercing to a string by definition, %s
is essentially just performing str[arg]
.
%d
on the other hand, is calling int[]
on the argument before calling str[]
, like str[int[arg]]
, This will cause int
coercion as well as
str
coercion.
For example, I can convert a hex value to decimal,
>>> '%d' % 0x15
'21'
or truncate a float.
>>> '%d' % 34.5
'34'
But the operation will raise an exception if the argument isn't a number.
>>> '%d' % 'thirteen'
Traceback [most recent call last]:
File "", line 1, in
TypeError: %d format: a number is required, not str
So if the intent is just to call str[arg]
, then %s
is sufficient, but if you need extra formatting [like formatting float decimal places] or other coercion, then the other format symbols are needed.
With the f-string
notation, when you leave the formatter out, the
default is str
.
>>> a = 1
>>> f'{a}'
'1'
>>> f'{a:d}'
'1'
>>> a = '1'
>>> f'{a:d}'
Traceback [most recent call last]:
File "", line 1, in
ValueError: Unknown format code 'd' for object of type 'str'
The same is true with string.format
; the default is str
.
>>> a = 1
>>> '{}'.format[a]
'1'
>>> '{!s}'.format[a]
'1'
>>> '{:d}'.format[a]
'1'
In this article, we will see the difference between %s and %d in Python. Here, we start with the proper explanation of one at a time, then both, and at last compare these.
What does %s do in Python?
The % symbol is used in Python with a large variety of data types and configurations. It is used as an argument specifier and string formatter. %s specifically is used to perform the concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.
How to Use %s in Python
The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following code illustrates the usage of the %s symbol :
Python3
name
=
"Geek"
print
[
"Hey, %s!"
%
name]
Output
Hey, Geek!
What does %d do in Python?
The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.
Python3
num
=
2021
print
[
"%d is here!!"
%
num]
Output
2021 is here!!
How to Use %d in Python
The decimal and rational numbers are rounded off to the absolute integral part and the numbers after the decimal are scraped off, that is only the integers are extracted. The following code illustrates the usage of %d with decimal and fractional numbers:
Python3
frac_num
=
8
/
3
print
[
"Rational number formatting using %d"
]
print
[
"%d is equal to 8/3 using this operator."
%
frac_num]
dec_num
=
10.9785
print
[
"Decimal number formatting using %d"
]
print
[
"%d is equal to 10.9785 using this operator."
%
dec_num]
Output
Rational number formatting using %d 2 is equal to 8/3 using this operator. Decimal number formatting using %d 10 is equal to 10.9785 using this operator.
Difference Between %s and %d?
We can use a combination of operators also within a single program. Before that first, clear some concepts by comparing as given below :
It is used as a placeholder for string values. | It is used as a placeholder for numeric values. |
Uses string conversion via str[] before formatting. | Uses decimal conversion via int[] before formatting. |
%s can accept numeric values also and it automatically does the type conversion. | In case a string is specified for the %d operator a type error is returned. |
Common Errors Using %s and %d
Example 1:
The %s automatically converts a numeric value to a string without throwing an error.
Python3
name
=
"Sita"
age
=
22
print
[
"Using %s "
]
print
[
"%s's age is %s."
%
[name,age]]
Output
Using %s Sita's age is 22.
Example 2:
The %d, however, can only be used for numeric values. Otherwise, an error is returned.
Python3
name
=
"Sita"
age
=
22
print
[
"Using %d"
]
print
[
"%d's age is %d."
%
[name,age]]
Error
Using %d Traceback [most recent call last]: File "", line 4, in TypeError: %d format: a number is required, not str