Saturday, November 16, 2024
Google search engine
HomeLanguagesPython String replace() Method

Python String replace() Method

String replace() in Python returns a copy of the string where occurrences of a substring are replaced with another substring. In this article, we will see how to replace string Python.

Replace() String Python Syntax

The replace string Python method in Python strings has the following syntax:

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. 

Python Replace() String Example

In this, we will see different examples of Python string replacement.

Example

Python3




string = "Good Morning"
new_string = string.replace("Good", "Great")
  
print(new_string)


Output

Great Morning

Python Replace() String all Instances of a Single Character

In this example, we are only replacing a single character from a given string. The Python string replacement approach using replace() method is case-sensitive, and therefore it performs a case-sensitive substring substitution, i.e. R in FOR is unchanged.

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)


Output

grrks FOR grrks
geeks FOR geeks

Time Complexity: O(n)
Space Complexity: O(n)

Python Replace() String all Instances of a String

Here, we will use Python string replacement approach using replace() to replace all the geeks with Lazyroar using replace() function.

Python3




string = "geeks for geeks \ngeeks for geeks"
  
print(string)
  
# Prints the string by replacing only
# 3 occurrence of Geeks
print(string.replace("geeks", "Lazyroar"))


Output

geeks for geeks 
geeks for geeks
Lazyroar for Lazyroar
Lazyroar for Lazyroar

Time Complexity: O(m*n)
Space Complexity: O(n)

Python Replace() String only a Certain Number of Instances

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))


Output

gaaks for gaaks gaaks gaaks gaaks
geas for geas geas geeks geeks

Time Complexity: O(n)
Space Complexity: O(n)

Python Replace() String using a list comprehension and join() method

Here, we will Python string replacement approach using replace() to Split the original string into a list of substrings using the split() method. 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)


Output

geexs for geexs 

Time Complexity: O(n)
Space Complexity: O(n)

RELATED ARTICLES

Most Popular

Recent Comments