I would suggest the following implementation, which uses msvcrtone char to read every time and acts on it (processing Enterand Backspace, if it occurs):
import msvcrt
def input_between_strings (s, t):
res = ''
while True:
print(s, res, t, sep = '', end = ' \r')
curr = msvcrt.getch()[0]
if curr == 13:
print()
return res
elif curr == 8:
res = res[:-1]
else:
res += chr(curr)
In your case, you can call it like
result = input_between_strings('>>> Write your name: (', ')')
source
share