manipulate string

Werner Schiendl n17999950.temp.werner at neverbox.com
Tue Oct 28 13:20:49 EST 2003


Hi,

Eric Brunel wrote:
> 
> Apparently, the OP wants to discard characters at indexes 1, 3, 5, 7, 
> etc... So, assuming Python version is 2.3, the correct solution seems to 
> be:
> 
>  >>> a='0123456789'
>  >>> print ' - '.join(a[::2])
> 0 - 2 - 4 - 6 - 8
> 

almost :-)

the original poster said he wanted

a = '0 - 2 - 4 - 6 - 8 - '

which means the last faded-out 9 is missing in your solution


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 -'


I think the trailing space is a typo of the OP...


hth

Werner





More information about the Python-list mailing list