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

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
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
Recursively replace characters in string
The simple solution is to use a normal loop. I see no advantage to recursion here. Maybe use the itertools functions to generate the possible replacements. More on reddit.com
🌐 r/learnpython
5
1
January 23, 2021
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
1 week ago - 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 ...
🌐
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 …
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
Flexiple
flexiple.com › python › python-replace-character-in-string
Python string.replace() – How to Replace a Character in a String - Flexiple - Flexiple
March 11, 2022 - The Python string.replace() method is a powerful tool for modifying strings by replacing a character, or sequence of characters, with a new character or sequence. This function is part of Python's string class, allowing developers to easily ...
🌐
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 - When using the .replace() Python method, you are able to replace every instance of one specific character with a new one. You can even replace a whole string of text with a new line of text that you specify.
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-replace-characters-in-a-string
Python Program to Replace Characters in a String
April 7, 2025 - Inside the For Loop, we are using the If Statement to check whether the String character is equal to ch or not. If true, this program replaces it with Newch · str1 = input("Please Enter your Own String : ") ch = input("Please Enter your Own Character : ") newch = input("Please Enter the New Character : ") str2 = '' for i in range(len(str1)): if(str1[i] == ch): str2 = str2 + newch else: str2 = str2 + str1[i] print("\nOriginal String : ", str1) print("Modified String : ", str2)
🌐
Scaler
scaler.com › home › topics › python string replace() method
Python String replace() Method - Scaler Topics
April 19, 2023 - The replace() function in python is used for replacing a single character or substring with a new character/substring. It returns an updated copy of the string while leaving the original string unmodified.
🌐
GitHub
gist.github.com › Quescol › 0de90bc088f35ffc6363813718accc51
Python-string-replace-character.py · GitHub
Python-string-replace-character.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
If pattern is an object with a Symbol.replace method (including RegExp objects), that method is called with the target string and replacement as arguments. Its return value becomes the return value of replace().
🌐
Pierian Training
pieriantraining.com › home › python tutorial: replace character in a string
Python Tutorial: Replace Character in a String - Pierian Training
April 27, 2023 - In this example, the old value is a space character (`’ ‘`), and the new value is an underscore (`’_’`). The `replace()` function returns a new string (`’Hello_World!’`) with the space replaced by an underscore. Overall, understanding how to work with strings in Python is essential for any programmer who wants to manipulate textual data.
🌐
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:]).
🌐
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.
🌐
Replit
replit.com › discover › how-to-replace-a-character-in-a-string-in-python
How to replace a character in a string in Python
February 6, 2026 - Build and deploy software collaboratively with the power of AI without spending a second on setup.
🌐
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 ...