Python 3.13 introduced a completely redesigned REPL with a much more modern feel. It's easy to overlook many of the features in the new Python REPL.
Let's explore the various hidden features the new Python REPL supports, with a focus on tips that are useful for everyday usage.
Let's start with the keyboard shortcuts the new Python REPL supports. This table is organized with the most useful shortcuts at the top.
| Shortcut | Type | Action | OS Support |
|---|---|---|---|
Left |
Navigation | Go left one character | All |
Right |
Navigation | Go right one character | All |
Up |
Navigation | Previous block or line | All |
Down |
Navigation | Next block or line | All |
Ctrl+P |
Navigation | Previous block | All |
Ctrl+N |
Navigation | Next block | All |
Ctrl+L |
Feature | Clear screen | All |
Ctrl+C |
Feature | Cancel / interrupt | All |
Ctrl+D |
Exit | Exit REPL (Sends EOF) | Linux/Mac |
Ctrl+Z Enter |
Exit | Exit REPL (Sends EOF) | Windows |
Tab |
Feature | Autocompletion | All |
Alt+Enter |
Feature | Run current code block | All |
Ctrl+R |
Feature | Search upward in history | All |
Ctrl+A |
Navigation | Go to beginning of line | Linux/Mac |
Home |
Navigation | Go to beginning of line | All |
Ctrl+E |
Navigation | Go to end of line | All |
End |
Navigation | Go to end of line | All |
Ctrl+K |
Deletion | Delete to end of line | All |
Ctrl+U |
Deletion | Delete to start of line | All |
Ctrl+Left |
Navigation | Go back one word | All |
Alt+B |
Navigation | Go back one word | All |
Ctrl+Right |
Navigation | Go forward one word | All |
Alt+F |
Navigation | Go forward one word | All |
Alt+Backspace |
Deletion | Delete to start of word | All |
Ctrl+W |
Deletion | Delete to start of word | All |
Alt+D |
Deletion | Delete to end of word | All |
F2 |
Feature | Enter history mode | All |
F3 |
Feature | Enter paste/edit mode | All |
F1 |
Feature | Enter help mode | All |
Ctrl+A does not work in the usual Windows Command Prompt or PowerShell (it selects all text instead) unless they are run within the new Windows 10 Terminal app.
On Linux and Mac, Ctrl+D can be used to exit the REPL, but on Windows you'll need to press Ctrl+Z and then press Enter afterward (to submit).
Using Ctrl+D on Linux/Mac or Ctrl+Z and Enter on Windows only works when the shortcut is pressed at a blank prompt.
Note that the above shortcuts assume you are working in the new Python REPL (3.13+).
On Python 3.12 the only shortcuts above that worked on Windows are:
Left and Right for navigating charactersUp and Down for navigating line-by-line historyCtrl+Z Enter for exitingCtrl+Left and Ctrl+Right for navigating wordsMost of those shortcuts also worked on Python 3.12 on Linux and Mac except:
Up and Down only navigated line-by-line historyAlt+Enter did nothingF2 and F3 did nothing because these modes were added in the new REPLCtrl+U) may differ slightly in their abilitiesThe above list of shortcuts isn't actually comprehensive. I deliberately left out some shortcuts that are tricky to use and which I don't recommend trying to learn.
Here are a few of the remaining shortcuts that you might use, but I highly doubt it.
| Shortcut | Action | Example |
|---|---|---|
Ctrl+Y |
Paste (yank) previously "killed" text | Use after any deletion command |
Alt+Y |
Cycle through "kill ring" | Press repeatedly after Ctrl+Y |
Ctrl+T |
Transpose with previous character | Swap o in improt to make import |
Alt+<digit> |
Repeat command N times | Moving left N characters |
All the "deletion" commands are actually "kill" commands (Ctrl+K, Ctrl+U, Alt+Backspace, Alt+D) which put deleted text into a "kill ring" that you can "yank" back from and cycle through.
This is a very Emacs-like feature and I don't find myself ever using it.
Using Alt+<digit> will load a digit argument, similar to the numeric argument system within Emacs.
Here are some examples:
Alt+3 Alt+D: delete to end of word 3 times (to delete to end of next 3 words)Alt+1 Alt+5 Left: navigate to the left 15 charactersAlt+8 Alt+0 =: enter 80 = charactersIf you accidentally press one of the "digit arg" Alt+<digit> commands, you can use Ctrl+G to cancel your input.
Using the Up and Down keys in the new Python REPL now navigates blocks of code instead of just lines of code.
There are some important things to note about this navigation:
Up to go to a previous block, make a change, and accidentally hit Up too many times to go to a previous block, and then hit Down, your changes will not be discardedCtrl+C, which will print KeyboardInterrupt and show a fresh promptF2)Ever wished you could copy just the code you typed into the REPL, without all the >>> and ... prefixes before each line?
You can use history mode for that!
Hitting F2 will enter history mode, which shows the code you typed into the REPL without showing the prompts or output.
Then you can copy-paste as usual.
F3)Occasionally you may also find "paste mode" helpful for editing blocks of code without accidentally running them by hitting Enter a couple times at the end of the block.
While editing a block of code (or within a fresh block) you can hit F3 to enter paste mode and then edit your block.
Once you're done, hit F3 again to go back to normal mode.
You'll never actually need paste mode for pasting because the new REPL supports a "bracketed paste" terminal feature which makes pasting just work as expected, so you can think of paste mode as edit mode.
_ VariableThe _ variable (if not deliberately set) will hold the value of the previously run statement, if there is one:
>>> 3 + 4
7
>>> _
7
This feature exists in both the old and new REPLs.
If you are just upgrading to the new Python REPL (3.13+) be sure to note that:
Up and Down keys now navigate code blocks (not just lines of code)Tab now indents to the next tab stop (using 4 spaces) instead of inserting a tab characterBackspace in or after line indentation deletes back to the previous tab stopexit or quit will exit the REPLThe REPL in Python 3.14 also includes syntax highlighting now. You can (unofficially) customize the Python REPL syntax highlighting as well.
Additionally, the Python 3.14 REPL now allows the Tab key to be used for autocompleting module names.
Type from coll and hit Tab and you'll likely see from collections.
Type import _ and hit Tab twice and you'll see a list of many _-prefixed module names.
Running python without any arguments will launch the Python REPL:
$ python
Python 3.14.0
Type "help", "copyright", "credits" or "license" for more information.
>>>
But that's not the only way to launch an interactive Python interpreter!
If you'd like poke around after a .py file has been run, you can pass the -i argument to Python to drop into interactive mode:
$ python -i fibonacci.py
>>>
>>> nth_fibonacci(10)
55
>>> phi
1.618033988749895
If you use Python's breakpoint() function to debug your Python code, you'll enter PDB (the Python debugger) which is a REPL-like environment.
PDB is not quite a REPL but it can run individual Python commands.
To enter a full Python REPL from PDB you can use the interact command:
(Pdb) interact
*interactive*
>>>
Also note that the default Python REPL is not the only interactive Python prompt that exists.
Python also includes IDLE, which is a graphical interactive prompt. It has some features the REPL does not and also includes some limitations the REPL does not. You can run IDLE on any machine with:
$ python -m idlelib
You could also use IPython, ptpython, or bpython instead. Or if you prefer to work in a web browser, use a Jupyter notebook. These are all REPL-like environments. Do note that many of these other REPL-like environments support slightly different keyboard shortcuts than the default Python REPL.
I often pop open a Python REPL to perform a calculation or play around with some code. I also spend much of my teaching time within a Python REPL.
If you spend quite a bit of time in a Python REPL as well, it's worth learning the features the REPL supports. Bookmark this page, try to remember the most useful keyboard shortcuts above, and feel free to share this resource with others.
For other useful REPL-related resources see inspecting Python objects, understanding help(), and the features of help().
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week.
Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.