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.
Top answer 1 of 4
11
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.
2 of 4
4
Lists are mutable, strings (and ints) are not. Look it up for yourself. It has to do with the way memory is allocated.
Python: How can I replace one specific character on a string while leaving the rest of the string as it was? Oct 18, 2023
r/learnprogramming 2y ago
can someone help me replace letters in a string whithout using the replace string method and using a function with a loop? Jul 8, 2021
r/learnpython 5y ago
03:28
Replace All Occurrences Of A String In A File | Python Example ...
01:32
Python 3 | String Replace for Beginners #coding #programming #python ...
04:57
New in Python 3.13: Replace - YouTube
01:50
How to replace multiple words in a string? Tekie Byte #35 - YouTube
01:46
PYTHON : Best way to replace multiple characters in a string? - ...
Tutorialspoint
tutorialspoint.com › python › string_replace.htm
Python String replace() Method
The Python String replace() method replaces all occurrences of one substring in a string with another substring. This method is used to create another string by replacing some parts of the original string, whose gist might remain unmodified.
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.
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.
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › replace.html
replace — Python Reference (The Right Way) 0.1 documentation
>>> "ABCAB".replace('AB', 'ab') 'abCab' >>> "ABCAB".replace('YZ', 'ab') 'ABCAB' >>> "ABCAB".replace('AB', 'ab', 1) 'abCAB' >>> "ABCAB".replace('AB', 'ab', 2) 'abCab'
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.
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.
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.
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.
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
Scaler
scaler.com › home › topics › python string replace() method
Python String replace() Method - Scaler Topics
April 19, 2023 - Replace function in Python is used for replacing a single character or substring with a new character/substring. Learn more on Scaler Topics.