What does %s mean in Python String Formatting?

Python has borrowed some of the formatting functions from the C language. In C language, a format pacifier %s is used to tell the printf function to print the given argument as a string, but do you know what does %s mean in Python? Let me explain it to you in detail.

What does %s mean in Python?

%s is used for string formatting in the Python language. The function guides the python interpreter on how to format the string text while printing it on the screen. The %s operator also allows you to add another string value into a string called concatenation of strings.

Related: SSL Module is not found in python.

Now, let’s have a detailed discussion on the usage of %s Python to have a better understanding of what actually is %s.

What is %s used for in Python strings?

The %s is a string literal that is used to specify a string or to add one or more specific strings into an existing string? You can do it in many ways.

1. Use of a single %s in Python 

You can print a variable having a specific value assigned to it in the existing string through %s. For example: Take a variable named customer_name assigned with your specific customer’s name. Then add it to a string printed to thank the customer for shopping at your mart.

customer_name = "John"
print("Thank you %s. We hope you will visit us again." %customer_name)
### output ###
Thank you John. We hope you will visit us again.

You can also directly assign the string value to %s the print statement. For example:

print("I have two %s" %('brothers')) 
### output ###
I have two brothers

How do you use multiple %s in Python?

You can also add multiple values to a string, but you should be careful about the number of values you want to add to your string and the number of values in the parentheses after the % operator at the end of the statement. They should be equal.
The statement will add the values in the parentheses in the string with the sequence mentioned within the parentheses. For example:

print("%s and %s went to %s yesterday" %('Max', 'Jim', 'Paris'))
### output ###
Max and Jim went to Paris yesterday

You can also add multiple variables with their specific assigned string values into an existing string, but you should be careful that the number of variables and the number of %s users in the print statement should be equal.
The print statement will take the values for %s in the sequence in which the variables are mentioned in parentheses. For example:

Take three different variables named Alice, mangoes, and apples, and then print a statement for the liking of the mentioned fruits by Alice.

val1 = 'Alice'
val2 = 'mangoes'
val3 = 'apples'
print("%s likes %s and %s" %(val1, val2, val3))
### Output ###
Alice likes mangoes and apples

Taking input in a print statement using %s in Python

%s allows you to take user input within the print statement. It is done by declaring a variable assigned to take your required input and then using this variable in the print statement with the help of %s. For example:

name = input("what is your name?")
print("My name is %s" %name)
  • The output of this statement will first print what is your name? 
using %s with multiple variables
  • Then you have to input your required input. Let’s suppose you entered Jack.
formating Python string using %s
  • After entering the input, you will press the Enter key, and the program will display My name is Jack.
use of %s in Python

Type conversion using %s in Python

%s automatically specifies the data type of the added value as a string if its data type is not a string previously. It can do an auto-type conversion from other data types to a string.  

Let’s take an example. Calculate the speed of the boy during the morning walk within a specific time and distance covered.

Let’s use the variables S for speed, D for distance, Loc for location, and T for time. So, as we know,  

S = D/T

So, let’s write the code.

T = 2
D = 11
Loc = 'park'

Now, calculate the speed of the boy.

S = (float(D) / float(T))

So, the values of distance and time are converted into floating points to perform division. Now, using the string formatting:

print("The boy walked in the %s for %s hours and covered a distance of %s kilometers with an average speed of %s kilometers per hour." % (Loc, T, D, S))

This statement will give the output as:

The boy walked in the park for 2 hours and covered a distance of 11 kilometers with an average speed of 5.5 kilometers per hour.

Formatting Python String using %s

Hence, we can see that %s operate automatically and converts the data type of speed to string while formatting time and distance values.

Sequencing %s in a dictionary in Python

You can sequence multiple variables using the %s placeholder in a dictionary. Let’s suppose that we took three variables and assigned them different values. Now, we have to print the Python dictionary in a specific sequence

dic = { 'var1' : 'visited', 'var2' : 'Maldives', 'var3' : 'I'}
final= "%(var3)s %(var1)s %(var2)s" %dic

The output will be I visited Maldives.

Python string formatting using dictionary

Using %s to format a list in Python String

You can also mention a list in your print statement by using the %s format specifier. For example:

We have to print the list of the first three integers. So, the code in Python will be:

fir = 'First three integers = '
list = [1, 2, 3]
final= "%s %s" %(fir, list)

The output of this statement will be the First three integers = [1, 2, 3].

Python string formatting using list


While learning what does %s mean in Python, we came to know that %s is an operator that allows you to add more strings and values in a string. As you see above, we can use it in various ways in the Python language.

But recently, the format() method has taken the place of %s specifier. So now, It is totally up to the developer what he wants to use to concatenate strings into strings.

Also, let us know how this article helped you learn about %s string?