The string replace() method returns a copy of the string where occurrences of a substring are replaced with another substring.
Example:
Python3
string = "Hello World"new_string = string.replace("Hello", "Good Bye")print(new_string) |
Good Bye World
What is String replace() Method?
String replace() is a built-in function in Python and it is used to replace a substring with another string. It will replace every occurrence of that substring, so it should be used with caution.
It does not change the original string but returns a new one. It is mostly used in string substitution.
String replace() Method Syntax
string.replace(old, new, count)
Parameters:
- old – old substring you want to replace.
- new – new substring which would replace the old substring.
- count – (Optional ) the number of times you want to replace the old substring with the new substring.
Return Value :
It returns a copy of the string where all occurrences of a substring are replaced with another substring.
How to Use String replace() Function
You can easily use the replace() function, you just need to call the function with a string object and pass the strings as a parameter. The first parameter is the substring you want to replace, and the second parameter is the string you want to replace with.
Let’s understand it better how to replace a string in Python with a simple example:
Python3
string = "Replace"new_string = string.replace("Replace", "Replaced")print(new_string) |
Replaced
More Examples of String replace() Method
In this section, we will see different examples of Python string replacement.
Example:
Python3
string = "Good Morning"new_string = string.replace("Good", "Great")print(new_string) |
Great Morning
1. Replace all Instances of a Single Character in a String
In this example, we are only replacing a single character from a given string. The Python string replacement approach using the replace() method is case-sensitive, and therefore it performs a case-sensitive substring substitution, i.e. R in FOR is unchanged.
Example:
Python3
string = "grrks FOR grrks"# replace all instances of 'r' (old) with 'e' (new)new_string = string.replace("r", "e" )print(string)print(new_string) |
grrks FOR grrks geeks FOR geeks
Time Complexity: O(n)
Space Complexity: O(n)
2. Replace all Instances of a String in a String
Here, we will use the Python string replacement approach using replace() to replace all the geeks with GeeksforGeeks using the replace() function.
Example:
Python3
string = "geeks for geeks \ngeeks for geeks"print(string)# Prints the string by replacing only# 3 occurrence of Geeksprint(string.replace("geeks", "GeeksforGeeks")) |
geeks for geeks geeks for geeks GeeksforGeeks for GeeksforGeeks GeeksforGeeks for GeeksforGeeks
Time Complexity: O(m*n)
Space Complexity: O(n)
3. Replace only a Certain Number of Instances in a String
In this example, we will use the Python string replacement approach using replace() replacing certain numbers of words. i.e. “ek” with “a” with count=3.
Python3
string = "geeks for geeks geeks geeks geeks"# Prints the string by replacing 'e' by 'a'print(string.replace("e", "a"))# Prints the string by replacing only 3 occurrence of 'ek' by 'a'print(string.replace("ek", "a", 3)) |
gaaks for gaaks gaaks gaaks gaaks geas for geas geas geeks geeks
Time Complexity: O(n)
Space Complexity: O(n)
4. Replace String using a List Comprehension and join() Method
Here, we will do string replacement without using replace(). First, we will split the original string into a list of substrings using the split() method. Then use a list comprehension to replace each occurrence of old_substring with new_substring.
Join the list of substrings back into a string using the join() method.
Python3
my_string = "geeks for geeks "old_substring = "k"new_substring = "x"split_list = my_string.split(old_substring)new_list = [new_substring if i < len(split_list)-1 else '' for i in range(len(split_list)-1)]new_string = ''.join([split_list[i] + new_list[i] for i in range(len(split_list)-1)] + [split_list[-1]])print(new_string) |
geexs for geexs
Time Complexity: O(n)
Space Complexity: O(n)
We have covered the definition, syntax, and examples of the string replace() method in Python. We also saw a technique to replace string without using the replace() function.
String replacement is a very important operation on strings. It is very easy and simple to use.
Read Other String Methods
Similar Reads:
- Replace all occurrences of a substring in a string
- Replace substring in list of strings
- Replace multiple characters at once
- Case insensitive string replacement


