⚡
usefultools.dev
</>

Python Syntax Highlighter – Code to HTML

NEW

Python syntax highlighter that turns code into colored HTML with inline styles or CSS classes, light and dark themes and optional line numbers. Free, in your browser.

Runs 100% in your browser — your input is never uploaded or stored on our servers.

Preview

1from dataclasses import dataclass
2
3
4@dataclass
5class Point:
6    x: float = 0.0
7    y: float = 0.0
8
9    def distance(self, other: "Point") -> float:
10        """Euclidean distance to another point."""
11        return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
12
13
14if __name__ == "__main__":
15    p = Point(3, 4)
16    print(f"distance = {p.distance(Point()):.1f}")  # 5.0

Self-contained: paste into a blog editor, email or CMS that strips <style> tags.

<pre style="margin:0;padding:12px 16px;background:#f6f8fa;color:#1f2328;font-family:ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;font-size:14px;line-height:1.5;tab-size:4;overflow-x:auto;border-radius:6px"><code><span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">1</span><span style="color:#cf222e">from</span> dataclasses <span style="color:#cf222e">import</span> dataclass
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">2</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">3</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">4</span><span style="color:#8250df">@dataclass</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">5</span><span style="color:#cf222e">class</span> <span style="color:#953800">Point</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">6</span>    x: <span style="color:#8250df">float</span> <span style="color:#cf222e">=</span> <span style="color:#0550ae">0.0</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">7</span>    y: <span style="color:#8250df">float</span> <span style="color:#cf222e">=</span> <span style="color:#0550ae">0.0</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">8</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">9</span>    <span style="color:#cf222e">def</span> <span style="color:#6639ba">distance</span>(<span style="color:#953800">self</span>, other: <span style="color:#0a3069">&quot;Point&quot;</span>) <span style="color:#cf222e">-&gt;</span> <span style="color:#8250df">float</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">10</span>        <span style="color:#0a3069">&quot;&quot;&quot;Euclidean distance to another point.&quot;&quot;&quot;</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">11</span>        <span style="color:#cf222e">return</span> ((<span style="color:#953800">self</span>.x <span style="color:#cf222e">-</span> other.x) <span style="color:#cf222e">**</span> <span style="color:#0550ae">2</span> <span style="color:#cf222e">+</span> (<span style="color:#953800">self</span>.y <span style="color:#cf222e">-</span> other.y) <span style="color:#cf222e">**</span> <span style="color:#0550ae">2</span>) <span style="color:#cf222e">**</span> <span style="color:#0550ae">0.5</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">12</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">13</span>
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">14</span><span style="color:#cf222e">if</span> <span style="color:#8250df">__name__</span> <span style="color:#cf222e">==</span> <span style="color:#0a3069">&quot;__main__&quot;</span>:
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">15</span>    p <span style="color:#cf222e">=</span> Point(<span style="color:#0550ae">3</span>, <span style="color:#0550ae">4</span>)
<span style="display:inline-block;min-width:2ch;margin-right:16px;text-align:right;color:#8c959f;user-select:none">16</span>    <span style="color:#8250df">print</span>(<span style="color:#0a3069">f&quot;distance = {p.distance(Point()):.1f}&quot;</span>)  <span style="color:#6e7781;font-style:italic"># 5.0</span></code></pre>
⚡

Quick Examples

Typical inputs and the results this tool produces.

Keyword
def
<span class="py-kw">def</span>
Builtin
len(x)
<span class="py-bi">len</span>(x)
Constant
None
<span class="py-const">None</span>
Comment
# note
<span class="py-com"># note</span>
📖

Overview

This Python syntax highlighter converts a snippet into colored HTML you can paste into a blog post, documentation page, newsletter or slide. It uses a real Python 3.12 lexer, so f-strings with nested quotes, raw and byte strings, triple-quoted docstrings, decorators, numbers like 1_000.5e3j, match/case soft keywords and builtins such as len or print are colored correctly instead of guessed with a regular expression.

Choose a light theme (GitHub Light, Solarized Light) or a dark one (One Dark, Monokai) and toggle line numbers. Copy the result in two forms: self-contained HTML with inline styles, which survives email clients and editors that strip stylesheets, or compact HTML with py-* classes plus a CSS block for sites where you control the stylesheet.

Line numbers are marked as not selectable, so readers can copy the code without them. All highlighting runs in your browser.

Why use this tool?

  • Accurate colors from a real Python tokenizer
  • Works in email and CMS editors via inline styles
  • Two light and two dark themes
  • Line numbers that are not copied with the code
  • No JavaScript needed on the page you paste into
⭐

Features

🎨

Four themes

GitHub Light, Solarized Light, One Dark and Monokai, previewed live.

🐍

Python-aware lexer

Handles f-strings, raw/byte strings, docstrings, decorators, soft keywords and def/class names.

✉️

Inline-style HTML

Every color is written into the markup so it renders anywhere, including email.

🧾

Classes + CSS

Lean markup with py-kw, py-str and friends, plus the matching stylesheet.

🔢

Line numbers

Optional, right-aligned and excluded from text selection.

🔒

Escaped output

Characters like < and & in your code are HTML-escaped, so the snippet cannot break the page.

🛠️

How to Use

1

Paste your code

Put the Python snippet into the Python code box.

2

Pick a theme

Choose the theme, tab width and whether to show line numbers.

3

Choose the format

Select Inline styles or Classes + CSS under the preview.

4

Copy and paste

Press Copy HTML (and Copy CSS) and paste it into your page.

🤔

Frequently Asked Questions