What does %s mean in a Python format string?
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
The following Python code illustrates the way of performing string formatting.
Simple use of %s
Python3
# declaring a string variablename = "Geek" # append a string within a stringprint("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
# declaring a string variablevar1 = "Geek!"var2 = "Geeks for Geeks" # append multiple strings within a stringprint("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
# declaring string variablesstr1 = 'Understanding'str2 = '%s'str3 = 'at'str4 = 'GeeksforGeeks' # concatenating strings but %s not equal to string variablesfinal_str = "%s %s %s %s" % (str1, str3, str4) # printing the final stringprint("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
# declaring string variablesstr1 = 'Understanding'str2 = '%s'str3 = 'at'str4 = 'GeeksforGeeks' # concatenating stringsfinal_str = "%s %s %s %s" % (str1, str2, str3, str4) # printing the final stringprint("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
# declaring string variables with dictionarydct = {'str1': 'at', 'str2': 'GeeksforGeeks', 'str3': 'Understanding', 'str4': '%s'} # concatenating stringsfinal_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct # printing the final stringprint("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
# declaring string variablesstr1 = 'Understanding'str2 = 'integers'str3 = 'at'str4 = 'GeeksforGeeks = ' # declaring list variableslst = [1, 2, 3] # concatenating strings as well as listfinal_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst) # printing the final stringprint("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]

