Python
print
bold text
text formatting
Python tips

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.

python
1BOLD = "\033[1m"
2RESET = "\033[0m"
3
4print(f"{BOLD}Build succeeded{RESET}")
5print("normal line")

Always reset after the bold segment, or later lines may stay styled.

A small helper keeps this tidy in lightweight scripts and internal tools:

python
1def bold(text: str) -> str:
2    return f"\033[1m{text}\033[0m"
3
4print(bold("Important"))

Windows terminals with colorama

ANSI support varies across Windows terminals. colorama normalizes the behavior:

python
1from colorama import init, Style
2
3init(autoreset=True)
4print(Style.BRIGHT + "Task complete")
5print("normal output")

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:

python
1from rich.console import Console
2
3console = Console()
4console.print("[bold]Deployment finished[/bold]")
5console.print("[bold red]Error:[/bold red] invalid token")

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:

python
1import os
2import sys
3
4
5def supports_style() -> bool:
6    if os.getenv("NO_COLOR"):
7        return False
8    return sys.stdout.isatty()
9
10
11def maybe_bold(text: str) -> str:
12    if supports_style():
13        return f"\033[1m{text}\033[0m"
14    return text
15
16print(maybe_bold("Sync complete"))

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:

python
from IPython.display import Markdown, display

display(Markdown("**Training complete**"))

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:

text
ERROR: Failed to connect to database

Weak:

text
same message in bold only

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:

bash
python app.py > output.log
cat -v output.log

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:

python
1class Formatter:
2    def __init__(self, enabled: bool):
3        self.enabled = enabled
4
5    def bold(self, msg: str) -> str:
6        if not self.enabled:
7            return msg
8        return f"\033[1m{msg}\033[0m"
9
10fmt = Formatter(enabled=True)
11print(fmt.bold("Release complete"))

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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms