As in 2.x, use str.replace().

Example:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
Answer from Ignacio Vazquez-Abrams on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The replace() method replaces a specified phrase with another specified phrase.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-replace
Python String replace() Method - GeeksforGeeks
1 week ago - Return Value: A new string with the specified replacements applied. The original string is not modified because strings in Python are immutable. Example 1: Here, only the first occurrence of a substring is replaced using the count parameter.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ replace
Python String replace()
If the old substring is not found, it returns a copy of the original string. ... # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt')) song = 'Let it be, let it be, let it be, let it be'
๐ŸŒ
Real Python
realpython.com โ€บ replace-string-python
How to Replace a String in Python โ€“ Real Python
January 15, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_replace.htm
Python String replace() Method
But, since we have passed the count as 0, this method does not modify the current string, instead of that, it returns the original value ("Learn Python from Tutorialspoint"). str = "Learn Python from Tutorialspoint" strreplace = str.replace("Python", "Java", 0) print("String after replacing: " + strreplace)
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ string-replace-method
Master Python's String Replace Method for Text Manipulation
Explore Python's string replace() method to update text seamlessly. Learn to replace substrings, fix user input, and process files with practical examples.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-replace-string
Python String replace ()
Original string : "apples pears peaches" Replaced string : "ammles mears meaches" In this tutorial of Python Examples, we learned about string replace() method, and how to use this string replace() method to replace the occurrences of a substring with another string, with the help of well detailed ...
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-replace
Python String replace() | DigitalOcean
August 4, 2022 - s = 'Java is Nice' # simple string replace example str_new = s.replace('Java', 'Python') print(str_new) # replace character in string s = 'dododo' str_new = s.replace('d', 'c') print(str_new)
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ replace-method-in-python
Python replace() Method
July 17, 2024 - Parsing: Break down strings into smaller components to extract relevant information. Text Analysis: Analyze and draw insights from textual data. The basic syntax for using the replace() method in Python is as follows:
๐ŸŒ
Python Basics
pythonbasics.org โ€บ home โ€บ python basics โ€บ python string replace() method
Python String replace() Method - pythonbasics. ...
If you don't save the output, the string variable will contain the same contents. Saving output is done using: s = function() ... An optional parameter is the number of items that will be replaced. By default its all. The program below replaces only the first item:
๐ŸŒ
Keploy
keploy.io โ€บ home โ€บ community โ€บ how to replace strings in python?
How to Replace Strings in Python: Methods, Examples & Best Practices
September 26, 2025 - Sometimes, you want to replace more than one character at once in a string with the same replacement character. For example, replacing every a, b, and c in a text with "*" to hide or mask them.
๐ŸŒ
Medium
medium.com โ€บ @tajammalmaqbool11 โ€บ python-string-replace-the-complete-guide-with-examples-dc81506ae084
Python String Replace: The Complete Guide with Examples | by Tajammal Maqbool | Medium
October 10, 2025 - In this guide, weโ€™ll explore how to use Python string replace, along with practical examples, best practices, and performance tips.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-string-replace-function-guide
PyTutorial | Python String Replace Function Guide
February 5, 2026 - You can limit how many replacements occur, starting from the beginning of the string. # Replacing with a count limit sentence = "The cat and the other cat are friends." # Replace only the first occurrence of "cat" modified_sentence = sentence.replace("cat", "dog", 1) print("Original:", sentence) print("Modified:", modified_sentence)
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-string-replace-how-to-replace-a-character-in-a-string
Python string.replace() โ€“ How to Replace a Character in a String
September 1, 2022 - Here's an example that replaces "JavaScript" with "PHP" in a string: str = "I love JavaScript. I prefer JavaScript to Python because JavaScript looks more beautiful" new_str = str.replace("JavaScript", "PHP") print(new_str) # I love PHP.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-string-replace
Python String Replace
March 25, 2025 - Here, we are changing l with M. ... If we change the argument values as (โ€˜oโ€™, โ€˜ABCโ€™), this method substitutes o with ABC from Hello World, and the output will be โ€˜HellABC WABCrldโ€™. In this example, we are using this to change all ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-string-replace
Python String Replace Tutorial | DataCamp
June 25, 2020 - Python provides you with the .replace() ... number that indicates how many occurrences to replace. ... In the example code below, you will replace the substring house with car....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-string-replace-function-in-python-for-substring-substitution
Python String.Replace() โ€“ Function in Python for Substring Substitution
January 24, 2022 - In that example there were four instances of the character o. Specifically, it was found in the words to, coding, on, and go. What if you only wanted to change two words, like to and coding, to contain a instead of o? To change only two instances of a single character, you would use the count parameter and set it to two: phrase = "I like to learn coding on the go" # replace only the first two instances of 'o' with 'a' substituted_phrase = phrase.replace("o", "a", 2 ) print(phrase) print(substituted_phrase) #output #I like to learn coding on the go #I like ta learn cading on the go
๐ŸŒ
Cisco
ipcisco.com โ€บ home โ€บ python string replace
Python String Replace | To change Python Strings with Replace! โ‹† IpCisco
December 27, 2021 - In this example, we will use โ€œParisโ€ as old parameter and โ€œIstanbulโ€ as new parameter. We will not use the third count parameter here. msg = "I would like to be in Paris. Because Paris has perfect historical places." newmsg = msg.replace("Paris", "Istanbul") print(newmsg) The output of this Python String Replace example will be like below:
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python string replace() method
Python String replace() Method - Scaler Topics
April 19, 2023 - We need to pass the old value and ... value with the new value. In this example, we will change the first two occurrences of 'f' with 'F' in the string used below in the code....