replace() Function in python replaces a string or substring with an alternative string, which can have different size. In this tutorial we will learn
- a simple string replacement by replace() function of python
Syntax of replace() Function in python:
str.replace(old, new , [count])
old – The old substring to be replaced
new- substring, which would replace old substring.
Count – optional argument , number of occurrences to be replaced
Example 1 of replace() Function in python
str1 = "python is my favourite language" print str1.replace("my","everyones")
replaces “my” with “everyone” so the output will be
python is everyones favourite language
Example 2 of replace() Function in python
str1 = "python is my favourite language" print str1.replace("a","A",1) print str1.replace("a","A")
- First replace() Function replaces “i” with “I” in first occurrence
- First replace() Function replaces “i” with “I” in all the occurrences
python is my fAvourite language
python is my fAvourite lAnguAge
python is my fAvourite lAnguAge