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 do I incorporate   in regular expressions ?
You can stick it in a string with hex escape codes. 160 would be '\xa0', if I can do math this late at night. See if your regex engine works if you just stick some of those in the literal. More on reddit.com
๐ŸŒ r/Python
13
10
June 18, 2014
How do you replace a character in a string with a single backslash?

r/learnpython is probably a better subreddit for these kinds of questions.

Having said that, your first example actually works, try:

print("apple".replace('l', '\\'))
More on reddit.com
๐ŸŒ r/Python
7
0
March 16, 2015
๐ŸŒ
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.
๐ŸŒ
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โ€ฆ
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 โ€ฆ
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ replace
Python String replace()
If the old substring is not found, it returns a copy of the original string. ... # replacing 'cold' with 'hurt' print(song.replace('cold', 'hurt')) song = 'Let it be, let it be, let it be, let it be'
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 documentation
Strings may also be created from other objects using the str constructor. Since there is no separate โ€œcharacterโ€ type, indexing a string produces strings of length 1.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_modify.asp
Python - Modify Strings
The strip() method removes any whitespace from the beginning or the end: a = " Hello, World! " print(a.strip()) # returns "Hello, World!" Try it Yourself ยป ยท The replace() method replaces a string with another string:
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ replace-multiple-characters-in-string-python
5 Ways to Replace Multiple Characters in String in Python
October 10, 2022 - Learn how to replace multiple characters in string in python. This includes replace() with Lists, Dictionaries, re module and translate() & maketrans().
๐ŸŒ
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.
๐ŸŒ
Jinja
jinja.palletsprojects.com โ€บ en โ€บ stable โ€บ templates
Template Designer Documentation โ€” Jinja Documentation (3.1.x)
Literals are representations for Python objects such as strings and numbers. The following literals exist: ... Everything between two double or single quotes is a string. They are useful whenever you need a string in the template (e.g. as arguments to function calls and filters, or just to extend or include a template). ... Integers are whole numbers without a decimal part. The โ€˜_โ€™ character can be used to separate groups for legibility.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-replace-characters-in-a-string-in-Python
How to replace characters in a string in Python - Quora
Answer (1 of 4): By using replace() method The replace() method replaces all occurrences of a character with the new one. For instance, if we have a string any_string and we use the expression any_string.replace(โ€˜aโ€™, โ€˜bโ€™), it will replace all occurrences of the character โ€˜aโ€™ in ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ replacing-characters-in-a-string-using-dictionary-in-python
Replacing Characters in a String Using Dictionary in Python - GeeksforGeeks
July 23, 2025 - In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement.
๐ŸŒ
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.