Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default substituting %s in python scripts

Sometimes in Python scripts I see lines like:

cmd = "%s/%s_tb -cm cond+line+fsm -ucli -do \"%s\""

Where is the %s in the above line substituted? Does Python have some stack of strings and it pops them and replaces %s?

like image 913
prahallada Avatar asked May 10 '11 17:05

prahallada


2 Answers

That would be later used in something like:

print cmd % ('foo','boo','bar')

What you're seeing is just a string assignment with fields in it which will later be filled in.

like image 170
John Gaines Jr. Avatar answered Oct 05 '22 15:10

John Gaines Jr.


Basics of python string formatting

Not a specific answer to your line of code, but since you said you're new to python I thought I'd use this as an example to share some joy ;)

Simple Example Inline With a List:

>>> print '%s %s %s'%('python','is','fun')
python is fun

Simple Example Using a Dictionary:

>>> print '%(language)s has %(number)03d quote types.' % \  
...       {"language": "Python", "number": 2}
Python has 002 quote types

When in doubt, check the python official docs - http://docs.python.org/library/stdtypes.html#string-formatting

like image 20
HurnsMobile Avatar answered Oct 05 '22 15:10

HurnsMobile