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'
The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform 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.
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 Python code illustrates the way of performing string formatting.
Simple use of %s
Python3
name
=
"Geek"
print
[
"Hey, %s!"
%
name]
Output
Hey, Geek!
Multiple %s
Multiple strings can also be appended within a single string using the %s operator. The strings are replaced in the order of their position in the brackets, where ever there is an %s sign. This is illustrated using the following code snippet :
Python3
var1
=
"Geek!"
var2
=
"Geeks for Geeks"
print
[
"Hello %s Are you enjoying being at %s for preparations."
%
[var1, var2]]
Output
Hello Geek! Are you enjoying being at Geeks for Geeks for preparations.
Mapping strings to %s
However, the number of occurrences of this operator must be equal to the number of strings to replace with after the % sign. Otherwise, an error of the type “TypeError: not enough arguments for format string” is thrown.
Python3
str1
=
'Understanding'
str2
=
'%s'
str3
=
'at'
str4
=
'GeeksforGeeks'
final_str
=
"%s %s %s %s"
%
[str1, str3, str4]
print
[
"Concatenating multiple strings using Python '%s' operator:\n"
]
print
[final_str]
Error
Traceback [most recent call last]:
File “/home/c7b65fabd2ad00163eba70bbc39685d3.py”, line 8, in
final_str = “%s %s %s %s” % [str1, str3, str4]
TypeError: not enough arguments for format string
Correct Code
Python3
str1
=
'Understanding'
str2
=
'%s'
str3
=
'at'
str4
=
'GeeksforGeeks'
final_str
=
"%s %s %s %s"
%
[str1, str2, str3, str4]
print
[
"Concatenating multiple strings using Python '%s' operator:\n"
]
print
[final_str]
Output
Concatenating multiple strings using Python '%s' operator: Understanding %s at GeeksforGeeks
Order %s using dictionary
The strings are printed in whatever order they are appended using the dictionary key in output.
Python3
dct
=
{
'str1'
:
'at'
,
'str2'
:
'GeeksforGeeks'
,
'str3'
:
'Understanding'
,
'str4'
:
'%s'
}
final_str
=
"%[str3]s %[str4]s %[str1]s %[str2]s"
%
dct
print
[
"Concatenating multiple strings using Python '%s' operator:\n"
]
print
[final_str]
Output
Concatenating multiple strings using Python '%s' operator: Understanding %s at GeeksforGeeks
List as a string for %s
A non-string operator can also be formatted using the %s symbol in Python. Tuples can also be both inserted and formatted using this operator.
Python3
str1
=
'Understanding'
str2
=
'integers'
str3
=
'at'
str4
=
'GeeksforGeeks = '
lst
=
[
1
,
2
,
3
]
final_str
=
"%s %s %s %s %s"
%
[str1, str2, str3, str4, lst]
print
[
"Concatenating multiple values using Python '%s' operator:\n"
]
print
[final_str]
Output
Concatenating multiple values using Python '%s' operator: Understanding integers at GeeksforGeeks = [1, 2, 3]