skip to navigation
skip to content

rstr 2.2.6

Generate random strings in Python

rstr is a helper module for easily generating random strings of various types. It could be useful for fuzz testing, generating dummy data, or other applications.

It has no dependencies outside the standard library, and is compatible with Python 3.

A Word of Caution

By default, rstr uses the Python random module to generate psuedorandom text. This module is based on the Mersenne Twister and is not cryptographically secure.

If you wish to use rstr for password-generation or other cryptographic applications, you must create an instance that uses SystemRandom.

For example:

>> from rstr import Rstr
>> from random import SystemRandom
>> rs = Rstr(SystemRandom())


The basic method of rstr is rstr(). At a minimum, it requires one argument, an alphabet of characters from which to create a string.

>>> import rstr
>>> rstr.rstr('ABC')

By default, it will return a string between 1 and 10 characters in length. You may specify an exact length by including it as a second argument:

>>> rstr.rstr('ABC', 4)

You can also generate a range of lengths by adding two arguments. In the following case, rstr will return a string with a randomly selected length between 5 and 10 characters.

>>> rstr.rstr('ABC', 5, 10)

It’s also possible to include particular characters in your string. This is useful when testing a validator to make sure that certain characters are rejected. Characters listed in the ‘include’ argument will always be present somewhere in the resulting string.

>>> rstr.rstr('ABC', include='&')

Conversely, you can exclude particular characters from the generated string. This is helpful when starting with a pre-defined population of characters.

>>> import string
>>> rstr.rstr(string.digits, exclude='5')

Note that any of the arguments that accept strings can also accept lists or tuples of strings:

>>> rstr.rstr(['A', 'B', 'C'], include = ['@'], exclude=('C',))

Other methods

The other methods provided by rstr, besides rstr() and xeger(), are convenience methods that can be called without arguments, and provide a pre-defined alphabet. They accept the same arguments as rstr() for purposes of specifying lengths and including or excluding particular characters.

The characters provided by string.letters in the standard library.
The characters provided by string.uppercase in the standard library.
The characters provided by string.lowercase in the standard library.
The characters provided by string.printable in the standard library.
The characters provided by string.punctuation in the standard library.
The characters provided by string.printable in the standard library, except for those representing whitespace: tab, space, etc.
The characters provided by string.digits in the standard library.
The characters provided by the concatenation of string.letters and string.punctuation in the standard library.
The characters provided by the concatenation of string.digits and string.punctuation in the standard library.
Characters commonly accepted in text input, equivalent to string.digits + string.letters + ‘ ‘ (the space character).
Characters that are safe for use in postal addresses in the United States: upper- and lower-case letters, digits, spaces, and the punctuation marks period, hash (#), hyphen, and forward-slash.
Characters safe (unreserved) for use in URLs: letters, digits, hyphen, period, underscore, and tilde.
Characters that are allowed for use in hostnames, and consequently, in internet domains: letters, digits, and the hyphen.


Inspired by the Java library of the same name, the xeger() method allows users to create a random string from a regular expression.

For example to generate a postal code that fits the Canadian format:

>>> import rstr
>>> rstr.xeger(r'[A-Z]\d[A-Z] \d[A-Z]\d')
u'R6M 1W5'

xeger works fine with most simple regular expressions, but it doesn’t support all Python regular expression features.

Custom Alphabets

If you have custom alphabets of characters that you would like to use with a method shortcut, you can specify them by keyword when instantiating an Rstr object:

>>> from rstr import Rstr
>>> rs = Rstr(vowels='AEIOU')
>>> rs.vowels()

You can also add an alphabet to an existing instance with the add_alphabet() method:

>>> rs.add_alphabet('odds', '13579')
>>> rs.odds()


You can combine rstr with Python’s built-in string formatting to produce strings that fit a variety of templates.

An email address:




A postal address:

"""{0} {1}
{2} {3}
{4}, {5} {6}
""".format(rstr.letters(4, 8).title(),
           rstr.letters(4, 8).title(),
           rstr.digits(3, 5),
           rstr.letters(4, 10).title(),
           rstr.letters(4, 15).title(),
File Type Py Version Uploaded on Size
rstr-2.2.6-py2.py3-none-any.whl (md5) Python Wheel 2.7 2017-01-26 17KB
rstr-2.2.6.tar.gz (md5) Source 2017-01-26 10KB