In Python you can easily substitute a string without having to do character by character replacement (looking at you C programming).
In fact, Python has a method named .replace() which is part of the string.
The syntax for the replace method is:
str.replace(old, new[, max])
Where the parameters are:
- old the string to be replaced
- new the new string
- max optional, maximum number of replacements
Replace example
Take a look at this example in the Python shell.
>>> s = "All work and no play makes jack a dull boy"
>>> s = s.replace("jack","Josephine")
>>> s = s.replace("boy","girl")
>>> s
'All work and no play makes Josephine a dull girl'
>>>
So strings get replaced and the method can be called multiple times on the same string.
By default it replaces each string it finds, with no limitation.
The following example shows the usage of replace() method.
>>> x = "for philip python programming is pragmatic and powerful"
>>> x = x.replace("p","P")
>>> x
'for PhiliP Python Programming is Pragmatic and Powerful'
>>>
But if you add the optional max parameter, you can limit the number of occurrences:
>>> x = "for philip python programming is pragmatic and powerful"
>>> x = x.replace("p","P",1)
>>> x
'for Philip python programming is pragmatic and powerful'
>>>
This also works on f-Strings (formatted-strings).
>>> a = 1
>>> b = 2
>>> x = f"a is {a} and b is {b}"
>>> x = x.replace("a ","A ")
>>> x
'A is 1 and b is 2'
>>> x = x.replace("b ","B ")
>>> b
2
>>> x
'A is 1 and B is 2'
>>>
Related links:
Top comments (0)