manipulate string
Werner Schiendl
n17999950.temp.werner at neverbox.com
Tue Oct 28 13:33:36 EST 2003
Hi,
Werner Schiendl wrote:
>
> the following works:
>
> >>> a = '0123456789'
> >>>
> >>> b = " ".join( [i % 2 and "-" or a[i] for i in range(len(a))] )
> >>> b
> '0 - 2 - 4 - 6 - 8 -'
>
>
if you prefer to use generators, you could use:
>>> a = '0123456789'
>>>
>>> def hide_every_2nd(x):
... while True:
... yield x.next()
... x.next()
... yield "-"
...
>>> b = " ".join(hide_every_2nd(iter(a)))
>>> b
'0 - 2 - 4 - 6 - 8 -'
hth
Werner
More information about the Python-list
mailing list