String replacing with another string with occurrence count

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('red','brown')
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)

No comments: