TLDR: Python Prompt Strings
Date: 2020-02-21 Source: https://arpitbhayani.me/blogs/python-prompts
Overview
Customize your Python interactive shell prompt! Learn how to personalize and format your >>> and ... prompts. The >>> we see when the Python interactive shell starts, is called the Prompt String.
Key Points
- The >>> we see when the Python interactive shell starts, is called the Prompt String.
- Dynamic prompt strings: The documentation states that if we assign a non-string object to ps1 or ps2 then Python prompts by calling str() on the object every time a prompt is shown.
- Usually, the prompt string suggests that the interactive shell is now ready to take new commands.
- Python has 2 prompt strings, one primary >>> and one secondary ... which we usually see when an execution unit (statement) spans multiline, for example: while defining a function
- Personalizing the prompt strings
- The prompt strings are defined in the sys module as ps1 and ps2 and just like any other attribute we can change the values of sys.ps1 and sys.ps2 and the changes take effect immediately and as a result, the prompt we see in the shell changes to the new value.