What does %s mean in a python format string?

768    Asked by AswiniLobo in Python , Asked on Apr 14, 2021

What does %s mean in Python? And also, what does the below code do?

For instance...

if len(sys.argv) < 2>

sys.exit('Usage: %s database-name' % sys.argv[0]) 

if not os.path.exists(sys.argv[1]): 

sys.exit('ERROR: Database %s was not found!' % sys.argv[1])

Answered by Aswini Lobo

If you are searching for an answer of “what does %s mean in python” Kindly be informed that the % is a string formatting syntax in Python. Here is a very simple example:

#Python2

name = raw_input("who are you? ")

print "hello %s" % (name,)

#Python3+

name = input("who are you? ")

print("hello %s" % (name,))

The %s token will help you to insert a string. Notice that the %s token is substituted by whatever values you pass to the string after the % symbol.



Your Answer

Interviews

Parent Categories