How to insert data in a file at a specific position?

Solution for How to insert data in a file at a specific position?
is Given Below:

A file ‘webiste.txt’ contains text:

welcome to geeksforgeeks

Python code:

f  =  open('website.txt', 'a')
f.seek(11)
f.write("Python")
f.close()

desired out: welcome to python geeksforgeeks

real output: welcome to geeksforgeekspython

when I am running this code in ‘a’ mode the data gets appended at the last position not at 11th position.
In ‘w’ mode the data gets entered at position 11th but rest of the data overwritten.

How can we add python at 11th index without overwriting?

First open with ‘r’ method to read content and then use ‘w’ to rewrite the content and the following code may help 🙂

with open('website.txt','r') as f:
    content = f.read()
with open('website.txt','w') as f:   
    f.write(content[:11] + "Python" + content[11:])