- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python replace() Function
The replace() function in Python is used when we need to replace some part of string (old phrase) with another (new phrase), in a string. For example:
str = "This is fresherearth" print("The string is:", str) str = str.replace("fresherearth", "Python Programming") print("\nNow the string is:", str)
The snapshot given below shows the sample output of above program, demonstrating the replace() function:
Python replace() Function Syntax
The syntax of replace() function in Python, is:
string.replace(oldPhrase, newPhrase, count)
The function replace() returns the new version of the string, after replacing the oldPhrase with newPhrase. The third parameter, that is the count parameter is optional, and is used to specify the occurrence of oldPhrase to replace.
Note: The default value of count is basically the all occurrence of oldPhrase to replace with newPhrase.
Python replace() Function Example
Here is an example of replace() function in Python. This program allows user to define the string as well as old and new phrases to perform the replace operation using, of course, the replace() function:
print("Enter the String: ", end="") str = input() print("\nEnter the Old Phrase to Replace: ", end="") oldPhrase = input() print("Enter the New Phrase: ", end="") newPhrase = input() str = str.replace(oldPhrase, newPhrase) print("\nThe string is:", str)
The sample run with user input that was not was only the was as string data, was as old phrase, and python as new phrase, to replace was with python:
And if you want to remove old phrase with new phrase, but only for particular number of occurrence. For example, to remove was with python for only 2 occurrences. Then use the following code:
str = str.replace(oldPhrase, newPhrase, 2)
Now the output with same user input, comes out to be:
Since there are 3 was in the given string, but we need to replace only 2 was, therefore the last was is still available in the string, after replacing was by python, for only 2 occurrences.
« Previous Function Next Function »