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.
Discussions

Python: How can I replace one specific character on a string while leaving the rest of the string as it was?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
12
6
October 18, 2023
Weird character encoding issue when attempting to fix some text.
I had some trouble with non-ascii characters showing up in tab and csv files I was working with. I found this thread on stackoverflow helpful. I ended up using the second solution. non-ascii More on reddit.com
🌐 r/learnpython
25
7
August 7, 2014
Jinja Replace()
You should instead "set" a jinja variable to Store the for result and use the Double brackets AFTer and out of the for loop to print the one variable More on reddit.com
🌐 r/flask
6
1
July 13, 2020
What the hell is this character?
This is where regular expressions come in import re inputarray = re.sub(r'\s', '', inlist).split(',') as regular expressions use \s to match any whitespace character. There are other tricks like inputarray = ''.join(c for c in inlist if not c.isspace()).split(',') but those are most often less efficient compared to regex. More on reddit.com
🌐 r/learnpython
8
4
September 6, 2018
🌐
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.
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)
🌐
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:
🌐
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:
🌐
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....
🌐
Analytics Vidhya
analyticsvidhya.com › home › beginner’s guide to replace() method in python
Beginner’s Guide to replace() Method in Python
April 15, 2024 - The `replace()` method is a built-in function that allows you to substitute a specified phrase with another in a string. It’s a non-destructive method, meaning it creates a new string without altering the original. Also Read: Top 10 Uses of Python in the Real World with Examples