21

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?

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
prahallada
  • 11,741
  • 4
  • 16
  • 6
  • 2
    "DOes python has some stack of strings and it pops them and replaces %s?" No. You need to find a line that has `cmd %` on it. Include that in your question too, please. – S.Lott May 10 '11 at 17:56

3 Answers3

22

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

HurnsMobile
  • 4,341
  • 3
  • 27
  • 39
  • 1
    Er. Does this answer his question? He wants to know how it's possible to have a string with `%s` in it without also specifying the replacements at the same time – Michael Mrozek May 10 '11 at 18:01
  • Not directly, I was more addressing his statement of being new to Python. Sometimes a little bit of information about WHY something works the way it does can be helpful! Cheers. – HurnsMobile May 10 '11 at 18:02
  • There is a mistake in your script. Remove the is from the print or from the arguments. Now it would output: 'python is is fun'. – Lynch May 10 '11 at 18:05
  • It answered my question. I thought the variable values has to be specified right there like perl. But this is different. In my script they have something like this at a different line, which i have not noticed. cmd = cmd % (self.agent.tmp_dir, arch_id, "%s.do" % self.agent.get_script(test.get_name())) Thank you so much for your answers. I got 3 answers in less than 5 minutes. Thank you so much – prahallada May 10 '11 at 18:14
21

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.

John Gaines Jr.
  • 11,174
  • 1
  • 25
  • 25
  • OK Thanks! Thats exactly what is happening. It is filled later in an another line. I am good at PERL and so i thought it has to be filled there. Thanks so much for answer – prahallada May 10 '11 at 18:13
20

It's being used for string interpolation. The %s is replaced by a string. You use the modulo operator (%) to do string interpolation. The string will be on the left side, the values to substitute for the various %s are on the right, in a tuple.

>>> s = '%s and %s'

>>> s % ('cats', 'dogs' )
<<< 'cats and dogs'

If you have just a single character you can forget the tuple.

>>> s = '%s!!!'

>>> s % 'what'
<<< 'what!!!'

In newer versions of python the recommend way is to use the format method of the string type:

>>> '{0} {1}'.format('Hey', 'Hey')
<<< 'Hey Hey'
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Zach Kelling
  • 52,505
  • 13
  • 109
  • 108