Explain replace(old, new [, max])
Answers
Answered by
1
The replace() method returns a copy of the string where all occurrences of a substring is replaced with another substring.
Explanation:
Python's built-in string function replace() returns a copy of the string where old occurrences were replaced with new ones, potentially limiting the number of replacements to max.
- Syntax - str.replace(old, new[, max])
- Parameters
- old − This is old string to be replaced.
- new − This is new substring, replacing old substring.
- max − If this optional argument max is given, only the occurrences of the first count shall be replaced.
- Return Value - This method returns a copy of the string replaced by new, with all occurrences of substring old. If the optional argument max is given, only the occurrences of the first count are substituted.
Answered by
1
The replace method is used in Python to replace the occurrence of old with new in a string in which we set the maximum condition.
Explanation:
#!/usr/bin/python
str = "my name is pisti.pisti is really fit and fine”
print str.replace("is", "nas")
print str.replace("is", "nas", 4)
- In the above example, old=is, new=nas and max is optional. Then the output of the above program would be
My name nas pnasti.pnasti nas really fit and fine.
- In the above example, old=is, new=nas and max=2. Then the output of the above program would be
My name nas pnasti.pisti is really fit and fine.
Learn more about Python
Write any two features of python that make it user friendly
brainly.in/question/9677174
In how many different ways, can you work in python?
brainly.in/question/9479066
Similar questions
English,
5 months ago
Environmental Sciences,
5 months ago
Computer Science,
11 months ago
Hindi,
11 months ago
Biology,
1 year ago