Replace
Larry Bates
larry.bates at websafe.com
Sat May 6 11:51:44 EDT 2006
Eric wrote:
> I have a string...
>
> str = "tyrtrbd =ffgtyuf == =tyryr =u=p ttttff"
>
> I want to replace the characters after each '=', what I ended up doing is
> somthing like this...
>
> buf = list(str)
> newchr = '#'
>
> count = 0
> for i in range(len(buf)):
> if buf[count] == '=':
> buf[count + 1] = newchr
> count = count + 1
> else:
> count = count + 1
>
> newstr = ''.join(buf)
>
> Is there a better, faster way of doing it? Using somthing like
> str.index() dosn't work because...
>
> str = "hello world"
> for i in str:
> print str.index(i)
>
> 0
> 1
> 2
> 2
> 4
> 5
> 6
> 4
> 8
> 2
> 10
>
> Every 'e' in "hello world" has the same index value. Am i missing somthing?
Split the string on '=', and join it back with '=#'.
s='=#'.join(s.split('='))
-Larry Bates
More information about the Python-list
mailing list