Personalize your Python Prompt

The >>> we see when the Python interactive shell starts, is called the Prompt String. Usually, the prompt string suggests that the interactive shell is now ready to take new commands.

Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

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

>>> def foo(a, b):
...     return a + b

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.

>>> import sys
>>> sys.ps1 = '::: '

From the example above we see that changing the value of sys.ps1 to ::: changes the prompt to :::.

As the interactive shell runs in a terminal, we can color and format it using bash color format as shown below

import sys
sys.ps1 = "\033[1;33m>>>\033[0m "
sys.ps2 = "\033[1;34m...\033[0m "

The code snippet above makes our primary prompt string yellow and secondary prompt string blue. Here's how it looks

Python colored prompt

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. Now we create some stateful and dynamic prompt by defining a class and overriding the __str__ method.

Below we implement IPython like prompt where execution statement number is stored in member line of the class and is incremented every time the primary prompt renders.

# -*- coding: utf-8 -*-
import sys

class IPythonPromptPS1(object):
  def __init__(self):
    self.line = 0

  def __str__(self):
    self.line += 1
    return "\033[92mIn [%d]:\033[0m " % (self.line)

sys.ps1 = IPythonPromptPS1()
sys.ps2 = "    \033[91m...\033[0m "

The above code snippet makes prompt look like this

ipython prompt

Setting new prompt strings every time the shell starts

We would not want to run this code snippet every time we start the shell and hence we use an environment variable PYTHONSTARTUP which holds the path of a readable file and is executed before the first prompt is displayed in interactive mode.

So we dump the code snippet in a file, say and export PYTHONSTARTUP as


Now every time, we start our Python interactive shell, it will execute the file and set the required prompt strings.


Combining everything mentioned above I have created a utility called py-prompts. Here is a glimpse of the themes that the package holds.

Pretty Python Prompts GIF

I hope you found this piece interesting. Python being an exhaustively extensible language made it super-easy for us to change the prompt strings and be creative with it. If you have a theme idea or have already personalized your prompt, share it with me @arpit_bhayani, I will be thrilled to learn more about it.

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 38000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.

Other essays that you might like

Constant Folding in Python

3917 reads 2021-01-10

Every programming language aims to be performant and Python is no exception. In this essay, we dive deep into Python int...

Making Python Integers Iterable

1790 reads 2020-06-14

In Python, Integers are not iterables but we can make them iterable by implementing __iter__ function. In this essay, we...

Integer Caching in Python

1824 reads 2020-05-17

To gain a performance boost and avoid reallocation of frequently used integers, Python creates singleton instances of sm...

Changing Python

1121 reads 2020-01-03

I changed the Python's source code and made addition incorrect and unpredictable. The addition operation will internally...

Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.

Paid Courses

System Design for Beginners

A masterclass that helps early engineers and product managers become great at designing scalable systems.

180+ learners

Details →

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

1000+ learners

Details →

Redis Internals

Learn internals of Redis by re-implementing some of the core features in Golang.

98+ learners

Details →

Free Courses

Designing Microservices

A free playlist to help you understand Microservices and their high-level patterns in depth.

823+ learners

Details →

GitHub Outage Dissections

A free playlist to help you learn core engineering from outages that happened at GitHub.

651+ learners

Details →

Hash Table Internals

A free playlist to help you understand the internal workings and construction of Hash Tables.

1027+ learners

Details →

BitTorrent Internals

A free playlist to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

692+ learners

Details →