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
# declaring a string variable name = "Geek" # append a string within a string 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
# declaring a string variable var1 = "Geek!" var2 = "Geeks for Geeks" # append multiple strings within a string 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
# declaring string variables str1 = 'Understanding' str2 = '%s' str3 = 'at' str4 = 'Lazyroar' # concatenating strings but %s not equal to string variables final_str = "%s %s %s %s" % (str1, str3, str4) # printing the final string 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
# declaring string variables str1 = 'Understanding' str2 = '%s' str3 = 'at' str4 = 'Lazyroar' # concatenating strings final_str = "%s %s %s %s" % (str1, str2, str3, str4) # printing the final string print ( "Concatenating multiple strings using Python '%s' operator:\n" ) print (final_str) |
Output
Concatenating multiple strings using Python '%s' operator: Understanding %s at Lazyroar
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 dictionary dct = { 'str1' : 'at' , 'str2' : 'Lazyroar' , 'str3' : 'Understanding' , 'str4' : '%s' } # concatenating strings final_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct # printing the final string print ( "Concatenating multiple strings using Python '%s' operator:\n" ) print (final_str) |
Output
Concatenating multiple strings using Python '%s' operator: Understanding %s at Lazyroar
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 variables str1 = 'Understanding' str2 = 'integers' str3 = 'at' str4 = 'Lazyroar = ' # declaring list variables lst = [ 1 , 2 , 3 ] # concatenating strings as well as list final_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst) # printing the final string print ( "Concatenating multiple values using Python '%s' operator:\n" ) print (final_str) |
Output
Concatenating multiple values using Python '%s' operator: Understanding integers at Lazyroar = [1, 2, 3]