How to print bold text in Python
Codemia Knowledge Hub ·
In a terminal, wrap the text in the ANSI escape code for bold (\033[1m) and the reset code (\033[0m). Whether that renders depends on the output environment: terminals, notebooks and log files handle styling differently, so a robust approach applies styles only when supported and falls back to plain text otherwise.
ANSI escape codes in the terminal
Most modern terminals treat ANSI code 1 as bold and 0 as reset.
Always reset after the bold segment, or later lines may stay styled.
A small helper keeps this tidy in lightweight scripts and internal tools:
Windows terminals with colorama
ANSI support varies across Windows terminals. colorama normalizes the behavior:
autoreset=True saves you from resetting manually on every line.
Larger CLI tools with rich
For more advanced command-line interfaces, rich is easier to maintain than raw ANSI strings:
It also supports tables, progress bars and syntax highlighting, which keeps output consistent across commands.
Plain text for logs and redirected output
Styled output is usually unwanted in files, CI artifacts or machine-parsed logs. Toggle styling based on the environment:
This keeps control characters out of stored logs.
Bold text in Jupyter notebooks
ANSI codes may not render as expected in notebook output. Use Markdown display instead:
Keep the meaning in the text, not just the style
Bold should support meaning, not carry it alone. Pair it with explicit labels such as ERROR, WARN or SUCCESS so the message survives when styling is disabled.
Good:
Weak:
This also keeps output accessible and machine-readable.
Where to test styled output
Before shipping CLI styling, check it in:
- macOS Terminal and a Linux shell.
- Windows PowerShell and Windows Terminal.
- CI logs with redirected output.
- A notebook, if applicable.
To check a log for control characters:
If escape sequences appear, disable styling when stdout is not a tty.
Centralizing styles in one utility
As a tool grows, keep style literals out of command handlers. One formatter keeps behavior consistent across commands and tests, and makes new styles easy to add:
Mistakes with bold output
- Forgetting the reset code, so the style leaks into later lines.
- Assuming every Windows environment renders ANSI sequences the same way.
- Writing styled output into machine-parsed logs.
- Relying on bold alone to communicate severity.
- Skipping the plain-text fallback for non-interactive runs.
Related reading
- How can I print multiple things fixed text and/or variable values on the same line, all at once?
- How can I print multiple things on the same line, one at a time?
- How can I print variable and string on same line in Python?
- How can I profile a multithread program in Python?
- How can I raise exception in main thread when using eventlet.GreenPool.spawn
- How can I randomly select choose an item from a list get a random element?
- How can I remove a key from a Python dictionary?
- How can I remove a pytz timezone from a datetime object?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.