It is a string formatting syntax (which it borrows from C).
Please see "PyFormat":
Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the
%splaceholder.
Here is a really simple example:
#Python 2
name = raw_input("who are you? ")
print "hello %s" % (name,)
#Python 3+
name = input("who are you? ")
print("hello %s" % (name,))
The %s token allows me to insert (and potentially format) a string. Notice that the %s token is replaced by whatever I pass to the string after the % symbol. Notice also that I am using a tuple here as well (when you only have one string using a tuple is optional) to illustrate that multiple strings can be inserted and formatted in one statement.
It is a string formatting syntax (which it borrows from C).
Please see "PyFormat":
Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the
%splaceholder.
Here is a really simple example:
#Python 2
name = raw_input("who are you? ")
print "hello %s" % (name,)
#Python 3+
name = input("who are you? ")
print("hello %s" % (name,))
The %s token allows me to insert (and potentially format) a string. Notice that the %s token is replaced by whatever I pass to the string after the % symbol. Notice also that I am using a tuple here as well (when you only have one string using a tuple is optional) to illustrate that multiple strings can be inserted and formatted in one statement.
Andrew's answer is good.
And just to help you out a bit more, here's how you use multiple formatting in one string:
"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
If you are using ints instead of string, use %d instead of %s.
"My name is %s and I'm %d" % ('john', 12) #My name is john and I'm 12
What does %d and %s do in Python and why do we use it?
file - What does %s" % stand for in a Python print statement - Stack Overflow
What does %s mean in Python?
It allows you to print a string in a string. So if you wanted to customize a "Hello Word" with a name you could do name = raw_input("Name?") print "Hello %s" % name
More on reddit.comWhat does %d and %s do in Python and why do we use it?
I'm a beginner learning python, but I came across this in my book:
password = adjective + noun + str(number) + special_char print ("Your new password is: %s" % password)
Can anyone please explain what is the use of the %s and % symbols? I searched google but still cant grasp the information.
Hello, so I am starting to learn python on my own learnpython.org and I am currently on the string formatting section and it starts talking about %d and $s. I kinda understand what it does but not really. However my main question is why do we use %s and %d. One example I have seen was
name = 'Geek
print ("Hey, %s,!" % name)
This printed out "Hey, Geek!". But why do we use it when we can also just do
print ("Hey", name)
As I am typing this out I kind of see the use but still any advice would be helpful
Edit: Thanks for all the replies they really helped!! I will be sending in thousands of more questions as I continue to learn.
I'm sorry for asking such a simple question. I am quite the beginner.
Edit: Thanks everyone!