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?
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 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
Replace character in string
Assuming that you're only working with single characters, you can create a dictionary that associates characters with strings. Iterate through the characters, and then join the resulting iterable. def replace_chars(dct, s): return ''.join(dct.get(c, c) for c in s) In the REPL: >>> replace_chars({"A" : "BC", "B" : "CD"}, "AB") 'BCCD' (Challenge: What if you want to replace an arbitrary number of characters? That is, "AB" should be replaced with "C", but if it only finds "A", it should replace with "D") More on reddit.com
🌐 r/learnpython
17
2
October 31, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-replace
Python String replace() Method - GeeksforGeeks
July 7, 2026 - replace() method is used to replace a specified substring with another substring in a string.
🌐
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…
🌐
Mimo
mimo.org › glossary › python › string-replace-method
Master Python's String Replace Method for Text Manipulation
You can also use replace() to replace single characters in a string rather than substrings.
🌐
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
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-different-characters-in-string-at-once
Python - Replace Different Characters in String at Once - GeeksforGeeks
July 15, 2025 - List comprehension iterates over ... characters back into a single string. This method uses re.sub() function from Python's re module to replace characters in a string....
🌐
GitHub
gist.github.com › Quescol › 0de90bc088f35ffc6363813718accc51
Python-string-replace-character.py · GitHub
Save Quescol/0de90bc088f35ffc6363813718accc51 to your computer and use it in GitHub Desktop. Download ZIP · Raw · Python-string-replace-character.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-replace
Python String replace() | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Replace Strings in Python: replace(), translate(), and Regex
May 4, 2025 - To swap characters, define a mapping and apply translate(). s = 'one two one two one' print(s.translate(str.maketrans({'o': 't', 't': 'o'}))) # tne owt tne owt tne print(s.translate(str.maketrans('ot', 'to'))) # tne owt tne owt tne ...
🌐
Medium
medium.com › @ryan_forrester_ › python-string-replace-how-to-guide-ac6884add7b3
Python String Replace: How To Guide | by ryan | Medium
October 23, 2024 - Also, while `replace()` is perfect for simple string substitutions, for more complex pattern matching and replacement, look into Python’s `re` module, which offers more advanced text processing capabilities through regular expressions.
🌐
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.
🌐
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
🌐
Facebook
facebook.com › Pythontutorial4u › posts › strings-in-python-explained-for-beginnerspython › 1426619946171371
Strings in Python Explained For Beginners #python
This content isn't available right now · When this happens, it's usually because the owner only shared it with a small group of people, changed who can see it or it's been deleted · Go to Feed · Go back · Visit Help Center
🌐
Quora
quora.com › How-do-you-replace-the-first-two-characters-of-a-string-in-Python
How to replace the first two characters of a string in Python - Quora
Answer (1 of 3): String are immutable in python.Either we can create another string or convert the into a character list. For ex. a = ‘abcdef' b = a[1]+a[0]+a[2:] c = list(a) c[0], c[1]=c[1], c[0]