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
Python Examples Python Compiler ... Python Interview Q&A Python Training ... The replace() method replaces a specified phrase with another specified phrase....
🌐
Reddit
reddit.com › r/learnpython › using replace() method
r/learnpython on Reddit: Using replace() method
February 27, 2024 -

Hi guys, I had a quick question about the exercise I was working on.

For using the replace() method and printing the result, we have to assign it to another variable and print that new variable.

sentence = sentence.replace(‘hey’, ‘hi’) print(sentence)

But for something such as the sort method we don’t need to assign it to a new variable and just use it straight up.

list = […] list.sort() print(list)

Why is it that some methods you need to assign it to a new variable, while others work while not assigning it to a new variable and the original is changed. Thank you.

Sorry about format, I’m on mobile.

Discussions

How to use string.replace() in python 3.x - Stack Overflow
Python documentation is an absolute shambles considering it is touted as an ideal first language. Quite often the stuff is there, but because of the poor way its organized, its often not indexed well by search engines or even their own site. Look at ToolMakerSteve's link, core string functions are lumpted in to standard types. This does not come up when you search for string functions. ... To be clear: string.replace... More on stackoverflow.com
🌐 stackoverflow.com
Struggling with replace method
I am trying to write this code to check if any of the ord values of the characters in the argument string are multiples of the number in the argument, and then remove them. For some reason it is only removing the h, even though if I ask it to print the values to be replaced it outputs b, d, ... More on discuss.python.org
🌐 discuss.python.org
4
0
April 8, 2024
Use replace function with list of strings python? - Stack Overflow
I have a bunch of replace functions to modify a list of strings that I have . Originally, I had written out each list comprehension of what strings I want to be replaced. ls = ['sentence with strin... More on stackoverflow.com
🌐 stackoverflow.com
Is there an easier way to replace multiple different things at once
On a high level, the method you’ve demonstrated is both idiomatic and the fastest (or one at least one of them). However, a bit of reorganization will make it both a little more efficient (by not leaving the file handle open as long), and will ensure it looks cleaner and more readable. More on discuss.python.org
🌐 discuss.python.org
4
1
March 14, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-replace
Python String replace() Method - GeeksforGeeks
July 7, 2026 - Explanation: replace("-", " ") replaces each hyphen (-) with a space. Comment · Python Fundamentals · Introduction1 min read · Input & Output2 min read · Variables4 min read · Operators4 min read · Keywords2 min read · Data Types4 min ...
🌐
Server Academy
serveracademy.com › blog › python-replace-function
Python Replace() Function Blog | Server Academy
November 14, 2024 - The method in Python is a powerful tool for working with strings, allowing you to replace parts of a string with new values. Whether you’re changing characters…
🌐
AskPython
askpython.com › python › string › python-replace-function
Python replace() function - AskPython
May 21, 2026 - str.replace(old, new) returns a copy of the string with all occurrences of the old substring replaced with the new substring. The original string stays untouched since strings are immutable in Python.
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › string-replace-method
Master Python's String Replace Method for Text Manipulation
text = "Error: User failed login" is_sensitive = True if is_sensitive: text = text.replace("User", "Anonymous") print(text) # Outputs: 'Error: Anonymous failed login' ... Become a Python developer. Master Python from basics to advanced topics, including data structures, functions, classes, and error handling
🌐
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.
🌐
Analytics Vidhya
analyticsvidhya.com › home › beginner’s guide to replace() method in python
Beginner’s Guide to replace() Method in Python
April 15, 2024 - Imagine you have a sentence, and you want to swap a word with another. That’s what replace() does. It finds the word you want to change and replaces it with the new word you choose.
🌐
StrataScratch
stratascratch.com › blog › how-to-replace-a-character-in-a-python-string
How to Replace a Character in a Python String - StrataScratch
October 18, 2024 - You can use this function to search patterns and replace them with specified characters. Its syntax is as follows. ... count: The maximum number of pattern occurrences to replace.
🌐
Real Python
realpython.com › replace-string-python
How to Replace a String in Python – Real Python
October 22, 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.
🌐
AlgoCademy
algocademy.com › blog › mastering-python-replace-a-comprehensive-guide-to-string-manipulation
Mastering Python Replace: A Comprehensive Guide to String Manipulation – AlgoCademy Blog
Remember, when using the replace method, it’s important to consider the context of your text to avoid unintended changes. Additionally, the pandas dataframe.replace() function is used to replace strings, regex, lists, dictionaries, series, numbers, etc., from a pandas dataframe in Python.
🌐
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.
🌐
Python.org
discuss.python.org › python help
Struggling with replace method - Python Help - Discussions on Python.org
April 8, 2024 - I am trying to write this code to check if any of the ord values of the characters in the argument string are multiples of the number in the argument, and then remove them. For some reason it is only removing the h, even though if I ask it to print the values to be replaced it outputs b, d, and f as well.
🌐
Python Academy
python-academy.org › functions › replace
replace — Python Function Reference
A comprehensive guide to Python functions, with examples. Find out how the replace function works in Python. Return a copy of the string with all occurrences of substring old replaced by new.
🌐
Stack Overflow
stackoverflow.com › questions › 70038079 › use-replace-function-with-list-of-strings-python
Use replace function with list of strings python? - Stack Overflow
I have a bunch of replace functions to modify a list of strings that I have . Originally, I had written out each list comprehension of what strings I want to be replaced. ls = ['sentence with strin...
🌐
Python.org
discuss.python.org › python help
Is there an easier way to replace multiple different things at once - Python Help - Discussions on Python.org
March 14, 2022 - im trying to use the .replace() mutiple times at once and i was wondering if there was a cleaner way to do it Code example: with open("user_data.txt", "w") as f: f.write(str(user_data).replace("{","{\n").replace("}","\n}").replace(",",",\n")) f.close() the code works fine but looks a bit messy
🌐
Note.nkmk.me
note.nkmk.me › home › python
Replace Strings in Python: replace(), translate(), and Regex
May 4, 2025 - Read, write, and create files in Python (with and open()) The replace() method replaces all occurrences of a substring with another.
🌐
Programiz
programiz.com › python-programming › methods › string › replace
Python String replace()
The replace() method returns a copy of the string where the old substring is replaced with the new string.
🌐
Quora
quora.com › In-Python-how-do-I-use-the-replace-function-on-strings-to-replace-multiple-characters-e-g-a-space-or-any-special-character-with-the-empty-string-E-g-Tes-ting-replace-only-replaces-the-space-not-the
In Python, how do I use the .replace() function on strings to replace multiple characters, e.g. a space or any special character, with th...
Answer (1 of 4): Why do you ask how to use a function (method) to do something after you’ve already demonstrated to yourself that the function/method doesn’t do that? Perhaps it’s better to describe what you want to accomplish and ask which functions or methods might already exist to ...