skip to content

Python One-Liners

Useful Python one-liners runnable directly from the shell with python -c or python -m. No file creation needed.

3 min read 30 snippets yesterday quick read

Python One-Liners#

All examples run from your terminal β€” no Python file needed. Assumes python points to Python 3.

Hello, World#

python -c "print('Hello, World!')"

Output:

Hello, World!

Current timestamp#

python -c "from datetime import datetime; print(datetime.now().isoformat())"

Output:

2026-04-25T14:32:07.843221

JSON pretty-print from stdin#

Pipe any JSON to Python for readable output:

echo '{"name":"Alice","age":30,"active":true}' \
  | python -c "import sys,json; print(json.dumps(json.load(sys.stdin), indent=2))"

Output:

{
  "name": "Alice",
  "age": 30,
  "active": true
}

Base64 encode / decode#

python -c "import base64; print(base64.b64encode(b'hello world').decode())"
python -c "import base64; print(base64.b64decode('aGVsbG8gd29ybGQ=').decode())"

Output:

aGVsbG8gd29ybGQ=
hello world

URL encode / decode#

python -c "from urllib.parse import quote; print(quote('hello world & more'))"
python -c "from urllib.parse import unquote; print(unquote('hello%20world%20%26%20more'))"

Output:

hello%20world%20%26%20more
hello world & more

Static file server#

Serve the current directory on port 8080:

python -m http.server 8080

Output:

Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
127.0.0.1 - - [25/Apr/2026 14:00:00] "GET / HTTP/1.1" 200 -

[!TIP] Add --directory /path/to/dir to serve a specific directory instead of the current one.

Benchmark a snippet#

python -m timeit -n 100000 "sum(range(100))"

Output:

100000 loops, best of 5: 2.38 usec per loop

Count lines / words in stdin#

cat /etc/hosts | python -c "import sys; lines=sys.stdin.readlines(); print(f'{len(lines)} lines, {sum(len(l.split()) for l in lines)} words')"

Output:

23 lines, 62 words

Generate a random password#

python -c "import secrets, string; print(secrets.token_urlsafe(16))"

Output:

Xt7mK3pN2qRsWvYu

Generate a UUID#

python -c "import uuid; print(uuid.uuid4())"

Output:

3f7c4b2e-1a8d-4e9f-b0c3-72d5e1f6a309

SHA-256 hash of a string#

python -c "import hashlib; print(hashlib.sha256(b'hello').hexdigest())"

Output:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

List pip packages as JSON#

pip list --format=json | python -c "import sys,json; pkgs=json.load(sys.stdin); [print(p['name'],p['version']) for p in pkgs[:5]]"

Output:

certifi 2024.2.2
charset-normalizer 3.3.2
click 8.1.7
fastapi 0.111.1
httpx 0.27.0

Flatten a nested list#

python -c "
nested = [[1,2],[3,[4,5]],6]
flat = list(__import__('itertools').chain.from_iterable(x if isinstance(x,list) else [x] for x in nested))
print(flat)
"

Output:

[1, 2, 3, [4, 5], 6]

Quick CSV to JSON#

python -c "
import csv, json, sys
reader = csv.DictReader(sys.stdin)
print(json.dumps(list(reader), indent=2))
" << 'EOF'
name,age,city
Alice,30,NYC
Bob,25,LA
EOF

Output:

[
  {
    "name": "Alice",
    "age": "30",
    "city": "NYC"
  },
  {
    "name": "Bob",
    "age": "25",
    "city": "LA"
  }
]

Interactive Python REPL with imports pre-loaded#

python -i -c "import json, os, sys, pathlib; from pathlib import Path; print('Ready. Path, json, os, sys loaded.')"

Output:

Ready. Path, json, os, sys loaded.
>>>