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 ... Q&A Python Bootcamp Python Training ... The replace() method replaces a specified phrase with another specified phrase....
Discussions

string - Python Replace \\ with \ - Stack Overflow
So I can't seem to figure this out... I have a string say, "a\\nb" and I want this to become "a\nb". I've tried all the following and none seem to work; >>> a 'a\\nb' >>> a.repla... More on stackoverflow.com
🌐 stackoverflow.com
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
Using replace() method
Strings are immutable. That is to say, after they've been created they cannot be modified. If you want to change them anyway, you have to make a copy that contains the changes you want. That's what .replace() does; it makes a new string that is a copy of the original with some portions of it replaced. As for why strings are immutable; there's a StackOverflow thread about that subject . On the other hand, lists are mutable (like most things). You can add, delete, modify, and rearrange its contents all you want, without having to create a new list to do it. So instead of creating a new list, .sort() just modifies the list in-place. More on reddit.com
🌐 r/learnpython
7
4
February 27, 2024
Str.replace of a set of characters - Ideas - Discussions on Python.org
I would like str.replace, when given a set of characters, to replace occurrence of any of the characters in the set. I.e. 'ASDFGH'.replace(set('SFH'), '') == 'ADG' should then hold. It would mean I would not have to go to the trouble of using the re module for a common string operation. More on discuss.python.org
🌐 discuss.python.org
0
December 1, 2019
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-replace
Python String replace() Method - GeeksforGeeks
1 week ago - A new string is returned with the updated values, original string s remains unchanged. ... count (optional): Specifies the maximum number of replacements to perform.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
Return a copy of the string with all occurrences of substring old replaced by new. If count is given, only the first count occurrences are replaced. If count is not specified or -1, then all occurrences are replaced.
🌐
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 - Replacing characters in a string with a list comprehension? That sounds awkward! Actually, it isn’t because it’s one of the three steps required in total: ... To convert a string to a list, use the split() method with this generic syntax. string_variable = 'your string here' list_of_words = string_variable.split() Now, list comprehension. It’s a Python method of creating a new list by applying an expression to each item in a list.
Find elsewhere
🌐
AlgoCademy
algocademy.com › blog › mastering-python-replace-a-comprehensive-guide-to-string-manipulation
Mastering Python Replace: A Comprehensive Guide to String Manipulation – AlgoCademy Blog
Combining replace with other string methods can lead to more efficient code. The replace() method in Python is a powerful tool for changing parts of a string. It allows you to substitute one substring with another, making it essential for text manipulation.
🌐
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.

🌐
AskPython
askpython.com › python › string › python-replace-function
Python replace() function - AskPython
May 21, 2026 - Whether I am stripping unwanted characters from a CSV import or standardizing user inputs, replace() handles it without ceremony. No regex needed for simple substitutions. The function has been part of Python’s string toolkit since the beginning, and it still solves 80% of my text transformation ...
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
The field_name is optionally followed by a conversion field, which is preceded by an exclamation point '!', and a format_spec, which is preceded by a colon ':'. These specify a non-default format for the replacement value. See also the Format specification mini-language section. The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. An arg_name is treated as a number if a call to str.isdecimal() on the string would return true.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Replace Strings in Python: replace(), translate(), and Regex
May 4, 2025 - Although Python does not have a built-in method to replace substrings at specific positions, you can achieve this by splitting the string with slicing and concatenating the parts with the replacement string.
🌐
Analytics Vidhya
analyticsvidhya.com › home › beginner’s guide to replace() method in python
Beginner’s Guide to replace() Method in Python
April 15, 2024 - 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 · The syntax of the `replace()` method is straightforward:
🌐
Python.org
discuss.python.org › ideas
Str.replace of a set of characters - Ideas - Discussions on Python.org
December 1, 2019 - I would like str.replace, when given a set of characters, to replace occurrence of any of the characters in the set. I.e. 'ASDFGH'.replace(set('SFH'), '') == 'ADG' should then hold. It would mean I would not have to go …
🌐
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.
🌐
YouTube
youtube.com › real python
Replacing a String In a String in Python - YouTube
This a preview of a video course about replacing strings in Python. If you’re looking for ways to remove or replace all or part of a string in Python, then t...
Published   August 24, 2023
Views   1K
🌐
FavTutor
favtutor.com › blogs › replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - For this approach, you can make use of for loop to iterate through a string and find the given indexes. Later, the slicing method is used to replace the old character with the new character and get the final output.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-replace-to-k-at-ith-index-in-string
Replace a String character at given index in Python - GeeksforGeeks
July 15, 2025 - Slicing is one of the most efficient ways to replace a character at a specific index. ... The string is split into two parts: everything before the target index (text[:index]) and everything after (text[index+1:]).
🌐
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'