Skip to main content

Generate randomized strings of characters using a template

Project description

Python package Documentation Status

Generate test data, unique ids, passwords, vouchers or other randomized textual data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings. A very simple invocation to produce a random string with word characters of 30 characters length:

from strgen import StringGenerator as SG

Full documentation

The current package requires Python 3.6 or higher. Use version 0.3.4 or earlier if you want to use Python 2.7 or an earlier Python 3 version.

NB: with version 0.4.2, the preferred method for generating a unique list is StringGenerator.render_set() instead of render_list(). Generate 50000 unique secure tokens in a few seconds:

secure_tokens = SG("[\p\w]{32}").render_set(50000)

render_set() does not support a progress callback.

There is a rich feature set to randomize strings in situ or include external data.

The purpose of this module is to save the Python developer from having to write verbose code around the same pattern every time to generate passwords, keys, tokens, test data, etc. of this sort:

my_secret_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(30))

that is:

  1. Hard to read even at this simplistic level.

  2. Hard to safely change quickly. Even modest additions to the requirements need unreasonably verbose solutions.

  3. Doesn’t use safe encryption standards.

  4. Doesn’t provide the implied minimal guarantees of character occurance.

  5. Hard to track back to requirements (“must be between x and y in length and have characters from sets Q, R and S”).

The template uses short forms similar to those of regular expressions. An example template for generating a strong password:


will generate something like the following:


Guarantee at least two “special” characters in a string:


You can also generate useful test data, like fake emails with plenty of variation:



From version 0.4.0, support for Python 2 is dropped. If you still need support for Python 2, use version 0.3.4.

There are no dependencies beyond the Python Standard Library.


Install as standard for Python packages from PyPi:

pip install StringGenerator


Released under the BSD license.


Thanks to Robert LeBlanc who caught some important errors in escaping special characters. Thanks to Andreas Motl for the progress counter.

Original Author:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

StringGenerator-0.4.3.tar.gz (11.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page