Strings in python are immutable, so you cannot treat them as a list and assign to indices.

Use .replace() instead:

line = line.replace(';', ':')

If you need to replace only certain semicolons, you'll need to be more specific. You could use slicing to isolate the section of the string to replace in:

line = line[:10].replace(';', ':') + line[10:]

That'll replace all semi-colons in the first 10 characters of the string.

Answer from Martijn Pieters on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse ... 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?
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
Str.replace of a set of characters - Ideas - Discussions on Python.org
It would mean I would not have to go to the trouble of using the re module for a common string operation. By extension, a set of multi-character strings should have any occurrences of the strings replaced, (in an indeterminate order, so watch out for replacement sets of strings were one string ... More on discuss.python.org
🌐 discuss.python.org
0
December 1, 2019
How to replace a character in a Python string?
Exemple: I have a string x = "hey" and I want to replace de 2nd character with "a" to turn x into "hay", is there any function that does it? obs: I'm using Python 3 More on sololearn.com
🌐 sololearn.com
2
0
Replacing certain characters in a string.
You can use str.translate method, like password = word.translte(str.maketrans('iamBo','!@M8.')) + 'q*s' More on reddit.com
🌐 r/learnpython
3
2
May 31, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-replace
Python String replace() Method - GeeksforGeeks
July 7, 2026 - 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. ... Explanation: s.replace("apple", "orange", 1) replaces "apple" only once due to count=1. Example 2: This example demonstrates that replace() treats uppercase and lowercase characters as different, replacing only exact matches.
🌐
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 - The basic method for replacing a string in Python is intuitively named str.replace(). It allows you to specify one character or a whole substring you want to replace and a character (or a substring) you want to replace it with.
🌐
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 …
Find elsewhere
🌐
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…
🌐
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.
🌐
Mimo
mimo.org › glossary › python › string-replace-method
Master Python's String Replace Method for Text Manipulation
The syntax of this method ensures clarity and flexibility, allowing developers to easily modify strings without altering the original data. In the Python programming languages, replace() is useful for replacing characters and other substrings within strings.
🌐
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.
🌐
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:]).
🌐
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 - The .replace() method returns a copy of a string. This means that the old substring remains the same, but a new copy gets created – with all of the old text having been replaced by the new text.
🌐
Unstop
unstop.com › home › blog › python string replace() | a detailed explanation!
Python String Replace() | A Detailed Explanation!
February 21, 2025 - In the Python code example above, we create a data variable and initialize it with the value- Hello Program!. Then, we create another variable, new_data, and assign the old string to it after replacing a single character.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
🌐
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 ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python replace character in string
Python Replace Character in String - Spark By {Examples}
May 21, 2024 - How to replace a character in a string in Python? To replace a character in a string using Python, you can utilize the str.replace() method. This method
🌐
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 - s = 'PHP is Nice' str_new = s.replace('PHP', 'Python') print(str_new) The program above is a simple string replace and replace character in string.