Python program to replace a substring in a string
Replace a substring in a string
In this page we will learn how to replace a substring in a string using python. In python we have a in-built function “replace” which helps to complete our task. Replace function takes three arguments ( two mandatory arguments and one is optional)
- str1= substring of a string which has to be replaced
- str2= substring of a string with which str1 has to be replaced
- count= number of times we need to replace , by default this value will be 1
Let’s replace a substring in a string using python
- Accept the inputs.
- Use replace function
- pass the arguments.
- Find the substring in the main string.
- Replace it with the new string.
- Print result.
Enter String :
PrepInsta Enter substring which has to be replaced :
Insta Enter substring with which str1 has to be replaced :
String after replacement PrepSter