Python String Replace Method

Introduction to Python String Replace

Python String replace() is a built-in Python method that returns a copy of the string in which all occurrences of a substring are replaced with another substring.

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: It returns a string copy with all occurrences of a substring replaced with another substring.

Note:

– If no count is specified, the old substring is replaced with the new substring in all occurrences.
– If the old substring is not found, it returns the copy of the original string.
– This function returns a copy of the string, without changing the original text.

Code Examples

Example 01: Python String replace()

string = "Welcome to Softhunt website, Softhunt is a website dedicated to programming courses"

print("Original String  :", string)
# replacing 'courses' with 'tutorials'
print("Replaced String: ", string.replace("courses", "tutorials"))
# replacing only 2 occurences of 'Softhunt'
print("Replaced with 2 occurences: ",string.replace("Softhunt", "Softhunt.net", 2))

Output:

Original String  : Welcome to Softhunt website, Softhunt is a website dedicated to programming courses
Replaced String:  Welcome to Softhunt website, Softhunt is a website dedicated to programming tutorials
Replaced with 2 occurences:  Welcome to Softhunt.net website, Softhunt.net is a website dedicated to programming courses

Example 02: More Examples of replace() method

string = "Welcome to Softhunt website, Softhunt is a website dedicated to programming courses"

print("Original String: ", string)
# replacing s by e
print("Replaced String: ", string.replace("s", "e"))

# 3 occurrence of m by a
print("Replaced with 3 occurrences: ", string.replace("m", "a", 3))

Output:

Original String:  Welcome to Softhunt website, Softhunt is a website dedicated to programming courses
Replaced String:  Welcome to Softhunt webeite, Softhunt ie a webeite dedicated to programming coureee
Replaced with 3 occurrences:  Welcoae to Softhunt website, Softhunt is a website dedicated to prograaaing courses

FAQs

How do you replace a character in a string in Python 3?

There are six common methods from which you can replace a character in a string

  1. Using slicing method
  2. Python String replace() method
  3. Using the list data structure
  4. Replace multiple characters with the same character
  5. Replace multiple characters with different characters
  6. Using regex module

How do I replace a string in a list?

If you wish to replace the string of a list’s items, use the string function replace() with the list comprehension for each element.

If there is no string to be replace, using replace() will not modify anything, thus you do not need to use an if condition to choose an element.

list = ['helloXXX', 'welcomeXXX', 'to999', 'SofthuntUUU']
replace = [list.replace('XXX', 'ZZZ') for list in list]
print(replace)

Output:

['helloZZZ', 'welcomeZZZ', 'to999', 'SofthuntUUU']

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Convert Int to String in Python by Using Four Different Methods

Leave a Comment