Например так:
def update_answer(file_path, question, new_answer):
with open(file_path, 'r') as file:
lines = file.readlines()
updated_lines = []
question_found = False
for line in lines:
if line.startswith(question):
updated_lines.append(f"{question} - {new_answer}\n")
question_found = True
else:
updated_lines.append(line)
#если вдруг не найден добавляем в конец, если не надо убрать
if not question_found:
updated_lines.append(f"{question} - {new_answer}\n")
with open(file_path, 'w') as file:
file.writelines(updated_lines)
# юзать так
file_path = 'answers.txt'
question = 'Вопрос#2'
new_answer = 'новый ответ'
update_answer(file_path, question, new_answer)