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 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?
str.replace replaces all instances of the search string with the replacement string, which isn't what you want the correct solution would be to turn the string into a list, update the value at the wanted index and then "".join(char_list) to rebuild the string 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
How to force Python to use Double quotes and not apostrophes for strings?

The single quotes are just there to show you that it's a string. They are not actually part of the string.

I can't think of an easy way to change that. You would have to make your own str class, I think.

More on reddit.com
๐ŸŒ r/learnpython
8
0
November 19, 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
People also ask

Why canโ€™t we replace a string in place in Python?
Strings in Python are immutable , meaning they cannot be modified after creation. Any replacement operation creates a new string object , leaving the original unchanged.
๐ŸŒ
keploy.io
keploy.io โ€บ home โ€บ community โ€บ how to replace strings in python?
How to Replace Strings in Python: Methods, Examples & Best Practices
Can I replace substrings using case-insensitive matching?
Yes, by using re.sub() with the flags=re.IGNORECASE parameter.
๐ŸŒ
keploy.io
keploy.io โ€บ home โ€บ community โ€บ how to replace strings in python?
How to Replace Strings in Python: Methods, Examples & Best Practices
How can I replace multiple substrings at once?
Use str.maketrans() with translate() , which allows mapping multiple characters or substrings efficiently.
๐ŸŒ
keploy.io
keploy.io โ€บ home โ€บ community โ€บ how to replace strings in python?
How to Replace Strings in Python: Methods, Examples & Best Practices
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-replace
Python String replace() Method - GeeksforGeeks
July 7, 2026 - 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'
๐ŸŒ
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)
๐ŸŒ
Real Python
realpython.com โ€บ replace-string-python
How to Replace a String in Python โ€“ Real Python
October 22, 2025 - As you can see, you can chain .replace() onto any string and provide the method with two arguments. The first is the string that you want to replace, and the second is the replacement. Note: Although the Python shell displays the result of .replace(), the string itself stays unchanged.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ replace-method-in-python
Python replace() Method
July 17, 2024 - The Python replace method is a built-in string method used to replace occurrences of a given substring within a string with a new substring.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ string-replace-method
Master Python's String Replace Method for Text Manipulation
replace() is a string method that replaces a stringโ€™s occurrences of a substring with another substring. The replace() method takes the old substring, the new substring, and an optional count parameter. When present, count specifies the number of occurrences to replace. ... Become a Python developer.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-string-replace
Python String Replace Methods | Using replace() and More
August 19, 2024 - This happens because in Python, '\\' is an escape character that tells Python to treat the next character as a literal character, not a special one. To correctly replace the newline character, we need to use a raw string, denoted by โ€˜rโ€™ before the string, or double backslashes. text = 'Hello\nWorld' new_text = text.replace('\\n', ', ') print(new_text) # Output: 'Hello, World' In this corrected example, weโ€™re using double backslashes to indicate that we want to replace the literal '\n', not the newline character.
๐ŸŒ
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:
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-replace
Python String replace() | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
๐ŸŒ
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.
๐ŸŒ
CodeConverter
codeconverter.com โ€บ articles โ€บ python-string-replace
Python String Replace โ€” Guide with Examples | CodeConverter Blog
February 11, 2026 - Maybe you want to replace only whole words, or ignore case. import re my_string = "I love coding in Java and JavaScript" new_string = re.sub(r'\bJava\b', 'Python', my_string) print(new_string) ... In this example, we use the \b word boundary marker to ensure we only match whole words.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-string-replace
Python String Replace
March 25, 2025 - Next, we used this Python string replace function to alter the old with the new. We are using the string replace function to change Backslash with forward-slash. text = r'C:\Programs\Example Code\xyz.txt' x = text.replace('\\', '/') print(x)
๐ŸŒ
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 - Let's take a look at another example. phrase = "I am learning Ruby. I really enjoy the ruby programming language!" #replace the text "Ruby" with "Python" substituted_text = phrase.replace("Ruby", "Python") print(substituted_text) #output #I am learning Python.
๐ŸŒ
Medium
medium.com โ€บ @pies052022 โ€บ python-string-replace-method-explanation-with-example-program-3fbd2f33f812
Python String replace() Method: Explanation with Example Program | by JOKEN VILLANUEVA | Medium
March 13, 2026 - If the old substring is not found, the original string is returned. s = 'PHP is Nice' str_new = s.replace('PHP', 'Python') print(str_new)
๐ŸŒ
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 Python, strings are immutable, meaning you canโ€™t change them directly. However, you can create a new modified string using the replace() method. Not worry!! ... If you donโ€™t specify the count, Python will replace all occurrences.
๐ŸŒ
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 ... by replacing the old 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....
๐ŸŒ
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....