Replace

E

Eric

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?
 
R

Ryan Forsythe

Eric said:
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)

So you want to turn "tyrtrbd =ffgtyuf == =tyryr =u=p ttttff" into
"tyrtrbd =#fgtyuf =# =#yryr =#=# ttttff"?

I'd probably end up writing it like you have (except the else is
redundant -- just end the test block and increment your counter in any case)
Is there a better, faster way of doing it? Using somthing like
str.index() dosn't work because...

str = "hello world"

You don't want to use str as a variable name -- it shadows the str()
builtin:
------------------------------------------------------------
Traceback (most recent call last):
for i in str:
print str.index(i) (snip)
Every 'e' in "hello world" has the same index value. Am i missing somthing?

Because `"hello".index("l")` returns the first place "l" appears in
"hello". It's always going to be 2.

--Ryan
 
V

vbgunz

pay attention to Ryan. Do not use 'str' as an identifier as you will
over write the built-in doing so. this seems easiest so far.

s = "tyrtrbd =ffgtyuf == =tyryr =u=p ttttff"
s = s.replace('=', '=#')
print s # -> tyrtrbd =#ffgtyuf =#=# =#tyryr =#u=#p ttttff
 
K

Kent Johnson

Eric said:
I have a string...

str = "tyrtrbd =ffgtyuf == =tyryr =u=p ttttff"

I want to replace the characters after each '=',

If you are replacing any char after = with # then re.sub() makes it easy:
In [1]: import re

In [2]: s = "tyrtrbd =ffgtyuf == =tyryr =u=p ttttff"

In [3]: re.sub('=.', '=#', s)
Out[3]: 'tyrtrbd =#fgtyuf =# =#yryr =#=# ttttff'

If the replacement char is not fixed then make the second argument to
re.sub() be a callable that computes the replacement.

PS str is not a good name for a string, it shadows the built-in str.

Kent
 
L

Larry Bates

Eric said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top