What does %s mean in a Python format string?
Last Updated :
29 Oct, 2022
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, wherever 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 <module>
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]
Please Login to comment...