The “%s” symbol is widely used in Python. This symbol represents a placeholder. The %s symbol is used to concatenate a string with another string. In this post, we will be learning about this “%s” format used in Python. So let’s get started.

How to Understand ‘%s’ in Python Format Strings?

The “%s” format is used for string concatenation purposes. We can format a string by using this placeholder. The number of values/strings we want to append or concatenate in the string should equal to the number of placeholders used in the main string. Let’s move towards examples of this format.

Example: Simple %s string format

A simple example of this format can be illustrated like this:

val = "string"
print("This is %s Format" % val)

The output of the above query will be:

As we can see, the “%s” placeholder is replaced by the string value defined in the code. 

In this similar way, we can append and concatenate the string to another string. We can also use multiple placeholders to append multiple values in a string. Let’s consider an example.

Example: Multiple %s string format

As declared earlier, we can also use multiple “%s” placeholders to append multiple into a string. Note that the number of placeholders used and the number of values should be equal. The values will be appended in the same sequence in place of the placeholders as they are written. The example is given below:

val_1 = "example"
val_2 = "string"
print("This is an  %s of %s Format . " % (val_1,val_2))

The output will simply append the “example” and “string” values in the place of both the “%s” sequentially. 

So this is all about the working of %s operator.

Conclusion

The %s operator or placeholder appends the string values into another string. We can concatenate a single or multiple values in a string using this operator. In the case of multiple %s operators, the values are sequentially appended in place of the placeholders as defined.