[Tutor] question on string
Kent Johnson
kent37 at tds.net
Mon Aug 1 20:06:00 CEST 2005
Gilbert Tsang wrote:
> Hi there,
>
> I would like to construct some string objects using the cprintf-style
> format:
>
> command_string = "diff -u %s %s > %s.patch" % ( src, dst, file )
>
> Of course it is illegal in python but I couldn't figure out a way to
> construct strings with that kind of formatting and substitution.
Actually that is correct as written! (though 'file' is a poor choice of variable name as it shadows the built-in file() function...)
>>> src = 'mySource'
>>> dst = 'myNewSource'
>>> file = 'diff.out'
>>> command_string = "diff -u %s %s > %s.patch" % ( src, dst, file )
>>> command_string
'diff -u mySource myNewSource > diff.out.patch'
See http://docs.python.org/lib/typesseq-strings.html for details on string formatting.
Kent
More information about the Tutor
mailing list