The library that bash uses to do this is the GNU readline library, which Python includes as part of the standard distribution:
https://docs.python.org/3/library/readline.html
A search term you might find useful is 'REPL', which stands for Read, Evaluate, Print, Loop, which is basically what bash or any similar tool does.