%s is an argument specifier and is used for string formatting. It borrows its syntax from the C language. Simply put, it lets you add a value inside a string.
The value can be a string or any object that can get converted into a string, for example, number, list, etc.
All string values
Consider the following example.
name = input("Please insert your name: ") song = input("What is your favorite song? " ) print("Hello %s! Would you like to listen to %s?" %(name, song))
In the above example, we take the name and the favorite song from the user and display a message using these values.
Moreover, we put %s as a placeholder at those places where you want values of variables.
A tuple containing values follows the format string, i.e., %(name, song). Remember to insert values in the same order you want to display them. In this case, the name will come first and then the song.
A sample output of the above example is given below.
Please insert your name: ashton What is your favorite song? Perfect Hello ashton! Would you like to listen to Perfect?
As you can see, this works as expected.
A single value
If we only have a single %s, then we can write a value without a tuple. Let’s see.
name = input("Please insert your name: ") print("Hello %s!" % name)
Output
Please insert your name: Agar Hello Agar!
Objects with a string representation
As already mentioned, a value can be any object that can get converted into a string. Let’s take an example.
name = "Smith" score = [70, 80, 90, 100] print("The score of %s in the last four matches: %s" % (name, score))
Output
The score of Smith in the last four matches: [70, 80, 90, 100]
As you can observe, we place a string and a list using the %s argument specifier. It converts the list to a string automatically.
Let’s take the same example and do it using the concatenation operator.
name = "Smith" score = [70, 80, 90, 100] print("The score of " + name + " in the last four matches " + str(score))
Output
The score of Smith in the last four matches [70, 80, 90, 100]
Here, unlike %s, we explicitly convert the list to a string and use + at every place we want to add a value.
TypeError
Moreover, the number of argument specifiers needs to be the same as the number of values in the tuple. If they are not, you will get an error. Let’s see.
name = input("Please insert your name: ") song = input("What is your favorite song? " ) print("Hello %s! Would you like to listen to %s?" %(name))
Output

What does %s in Python mean
As you can see in the above output, the program throws a TypeError.
Mapping key
Instead of remembering the order in which you want to insert values, you can pass a mapping key to %s. Consider the following example to understand this concept.
name = "Ashton Agar" age = 20 print("My name is %(name)s and my age is %(age)s." %{"age":age, "name":name})
Output
My name is Ashton Agar and my age is 20.
In the above example, we pass a dictionary containing (key, value) pairs instead of a tuple. Moreover, the key is placed in between the % and s, which gets replaced by its value later. Therefore, we do not need to remember the order.
%s is an old method of formatting strings. Better techniques such as format() and f-strings have got introduced that are easier to use and provide more functionalities.

Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!
Google
Sunday 30th of January 2022
Google
The information talked about inside the article are some of the ideal readily available.