What does %s mean in Python? And what does the following bit of 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])
It is a string formatting syntax (which it borrows from C).
Please see "PyFormat":
Here is a really simple example:
The
%stoken allows me to insert (and potentially format) a string. Notice that the%stoken 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.