rstr: Generate Random Strings

3.2.2 · active · verified Fri Apr 10

rstr is a Python helper module designed for easily generating random strings of various types. It is suitable for applications such as fuzz testing, generating dummy data, or creating random values based on regular expressions. The library is currently at version 3.2.2 and is actively maintained.

Warnings

Install

Imports

Quickstart

Demonstrates basic random string generation, fixed-length generation, regex-based generation with `xeger`, and how to use a cryptographically secure random number generator.

import rstr
from rstr import Rstr, xeger
from random import SystemRandom

# Generate a random string from a custom alphabet
print(f"Random string from 'ABC': {rstr.rstr('ABC')}")

# Generate a fixed-length random string
print(f"4-char string from '0123456789': {rstr.rstr('0123456789', 4)}")

# Generate a random string matching a regex pattern
print(f"Random string matching '[a-z]{{5,10}}': {xeger(r'[a-z]{5,10}')}")

# Use a cryptographically secure random source for sensitive cases
cs_rstr = Rstr(SystemRandom())
print(f"Secure random string (16 hex chars): {cs_rstr.rstr('0123456789abcdef', 16)}")

view raw JSON →