Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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])

2 Answers

0 votes
by (106k points)
edited by

This %s is a string formatting syntax in Python which is borrowed from C.

If you wish to know more, you can go through the following video:

0 votes
by (108k points)

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.

Related questions

Browse Categories

...