[Tutor] what does %s do?

Andrew Wilkins toodles@yifan.net
Fri, 26 Jan 2001 16:15:38 +0800


Hi Michael,

Okey dokey... with string manipulation comes the use of %. This prefixes
other letters, such as in %s (which by the way is short for string). It
replaces the %s in the string with whatever follows the % following the
string. So with a string '%sbuffer' % ('one'), the resulting string will
be 'onebuffer'. So it goes through a, which I assume is a list of
strings: ['one','two','three']. It goes through and executes the string
'onebuffer = []', 'twobuffer = []', 'threebuffer = []'.

I can't give you information on all the string substitution prefixes,
sorry, I'm not at one with my Python documentation.

Hope you can understand my odd way of thinking...

Andrew Wilkins

> -----Original Message-----
> From: tutor-admin@python.org
> [mailto:tutor-admin@python.org]On Behalf Of
> michaelbaker@operamail.com
> Sent: Friday, 26 January 2001 2:26
> To: tutor@python.org
> Subject: [Tutor] what does %s do?
>
>
>
> >hi tutors.
>
>
> I 've been going through some code sent here from arcege
> (thanks arcege) -
> it works great, but I wonder what %s does??
> the code:
>
>  >>> a = [ 'one', 'two', 'three' ]
>  >>> dir()
> ['__builtins__', '__doc__', '__name__', 'a']
>  >>> for b in a:
> ...   exec '%sbuffer = []' % b   #here it is
> ...
>  >>> dir()
> ['__builtins__', '__doc__', '__name__', 'a', 'b', 'onebuffer',
> 'threebuffer', 'twobuffer']
>  >>>
>
> as usual, thanks for the help
> m baker
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>