You can, of course, do what you ask, as you presented:
# read file and assign lines as list to variable stacktest_dict
# stripping line feed from each line
with open('stacktest.py') as stacktest:
stacktest_list = [line.strip() for line in stacktest.readlines()]
# replace string in chosen line with new string
for line in stacktest_list:
if line == "b = 'hotel'":
index = stacktest_list.index(line)
line = line.replace('hotel', 'new value')
stacktest_list[index] = line
# write lines with linefeeds added back to file
# using an f-string
with open('stacktest.py', 'w') as stacktest:
stacktest.writelines([f"{line}\n" for line in stacktest_list])
This is a very basic, rudimentary solution, meant to show that it can be done.
However, Python offers much better, and much more efficient and performative approaches. Personally, assuming you are creating everything, and there aren't esoteric reasons for doing things as presented in the question, I'd use a dictionary, stored as JSON, along the following lines:
# Create dict and write to file stacktest_dict.py
stacktest_list = {'a': 'home', 'b': 'hotel', 'c':'flat'}
with open('stacktest_dict.py', 'w') as stacktest_dict:
json.dump(stacktest_list, stacktest_dict)
# read file and assign to variable stacktest_dict
with open('stacktest_dict.py', 'r') as stacktest_dict_in:
stacktest_dict = json.load(stacktest_dict_in)
# assign new value for key 'b' in dict
stacktest_list['b'] = 'new value'
# write dict back to file
with open('stacktest_dict.py', 'w') as stacktest_dict:
json.dump(stacktest_list, stacktest_dict)
# read file and assign to variable stacktest_dict
with open('stacktest_dict.py', 'r') as stacktest_dict_in:
stacktest_dict = json.load(stacktest_dict_in)
# print the dict
print(stacktest_list)
# print the value of dict key 'b'
print(stacktest_list['b'])
In this scenario, you are reading and writing a JSON file, and working with the data as a native object that Python understands (a dictionary). This approach will save you time, both personally and computationally, and headaches going forward.
The scenario you've suggested, I'd present, is a perfect case for using a dictionary, stored, and read, as JSON. For further reference, see:
https://docs.python.org/3/tutorial/datastructures.html#dictionaries