The problem is that you are not doing anything with the result of replace. In Python strings are immutable so anything that manipulates a string returns a new string instead of modifying the original string.
line[8] = line[8].replace(letter, "")
Answer from Matti Virkkunen on Stack OverflowW3Schools
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
Top answer 1 of 3
127
The problem is that you are not doing anything with the result of replace. In Python strings are immutable so anything that manipulates a string returns a new string instead of modifying the original string.
line[8] = line[8].replace(letter, "")
2 of 3
1
I would use the translate method without translation table. It deletes the letters in second argument in recent Python versions.
def remove_chars(line):
line7=line[7].translate(None,'abcd')
return line[:7]+[line7]+line[8:]
line= ['ad','da','sdf','asd',
'3424','342sfas','asdfaf','sdfa',
'afase']
print line[7]
line = remove_chars(line)
print line[7]
Making str.replace() accept lists - Ideas - Discussions on Python.org
Syntax of the method: str.replace(old, new, count=-1) What if replace could accept two lists instead of two strings. Often I find in code: text.replace(a, b).replace(c, d) The concatenation of replace calls can cause a unnecessary performance hit if the string is large or the call is the calls ... More on discuss.python.org
Selective replacement of matching text in a string?
I’ve having to deal with 1000’s of rows of strings that that can take any of the following form: a single word a string of words a string of words separated by a delimiter (in this case \\) I need to be able to replace all instances of matching text with another value, being sure not to ... More on discuss.python.org
"replaceAll is not a function". Is that so?
Why do you think I see this weird message “replaceAll is not a function”? It definitely is! More on discuss.codecademy.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
Videos
03:28
Replace All Occurrences Of A String In A File | Python Example ...
13:47
Replacing a String In a String in Python - YouTube
12:54
Replace all Occurrences of a String in Python (3 Simple Steps!) ...
How To Replace One Character In A String In Python
01:32
Python 3 | String Replace for Beginners #coding #programming #python ...
00:58
Replace Letters In A String With Python #Coding #Python #casedigital ...
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.
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
May 25, 2026 - However, Unicode strings and 8-bit strings cannot be mixed: that is, you cannot match a Unicode string with a bytes pattern or vice-versa; similarly, when asking for a substitution, the replacement string must be of the same type as both the pattern and the search string. Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.
Educative
educative.io › answers › how-to-replace-all-occurrences-of-a-character-in-a-python-string
How to replace all occurrences of a character in a Python string
To replace all occurrences of a character in a string in Python, use the replace method and the re.sub method.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript | MDN
The original string is left unchanged. const paragraph = "This dog's name is just Dog! Yes, that is the name."; console.log(paragraph.replaceAll("name", "nickname")); // Expected output: "This dog's nickname is just Dog! Yes, that is the nickname." console.log(paragraph.replaceAll(/\bis\b/g, "was")); // Expected output: "This dog's name was just Dog!
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.
Python.org
discuss.python.org › python help
Selective replacement of matching text in a string? - Python Help - Discussions on Python.org
January 2, 2024 - I’ve having to deal with 1000’s of rows of strings that that can take any of the following form: a single word a string of words a string of words separated by a delimiter (in this case \\) I need to be able to replace all instances of matching text with another value, being sure not to ...
Sololearn
sololearn.com › en › Discuss › 2541803 › solved-why-replaceall-method-does-not-work-on-sl
[SOLVED] Why replaceAll method does not work on SL
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
Reddit
reddit.com › r/learnprogramming › python: how can i replace one specific character on a string while leaving the rest of the string as it was?
r/learnprogramming on Reddit: Python: How can I replace one specific character on a string while leaving the rest of the string as it was?
October 18, 2023 -
word1 = input("Word: ") # lets say that the given word is "tower"
word2 = "********************"
word2 = word2.replace(word[3], word[3])
print(word2)
# Now the my code replaces all the "*" characters with the character "e"
# It just prints "eeeeeeeeeeeeeeeeeeee"
# I would like the code only to replace the 4th character with the other strings 4th
# I'd like it to print "***e****************"
Top answer 1 of 4
13
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
2 of 4
13
You might consider using the index notation to build the new string out of substrings, something like this: str3 = str1[0:3] + str2[4] + str1[5:]