First page Back Continue Last page Overview Graphics

String Substitution

To substitute a substring old by a substring new we can use the method replace:

s.replace(old, new[, count])

Without using the optional parameter count every occurence of old will be replaced by new.

>>> s = "jkh abc jklj abc jkkj klj abc jlj abc iii"

>>> s.replace("abc", "---")

'jkh --- jklj --- jkkj klj --- jlj --- iii'

The optional parameter count can be used to control how many occurences of old will be replaced by new.

>>> s.replace("abc", "---", 3)

'jkh --- jklj --- jkkj klj --- jlj abc iii'