String handling and the percent operator
Erik Max Francis
max at alcyone.com
Thu Jul 13 20:03:16 EDT 2006
Tom Plunket wrote:
> For enrichment purposes, is there a way to do this sort of thing with
> a generator? E.g. something like:
>
> def SentenceGenerator():
> words = ['I', 'have', 'been', 'to', 'the', 'fair']
> for w in words:
> yield w
>
> message = "%s %s %s %s"
>
> print message % SentenceGenerator()
>
> (I ask because the above doesn't work)?
Use tuple(SentenceGenerator()). A generator is just another object, so
using it with the % operator tries to substitute it at one value. (Even
with this fix, though, your message didn't have enough formatters.)
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Human salvation lies in the hands of the creatively maladjusted.
-- Dr. Martin Luther King, Jr.
More information about the Python-list
mailing list