# How to Implement a Random String Generator With Python

In this post, you'll learn how to create a random string in Python using different methods; but, beware! Some of them only work with Python 3.6+.

By the end of this article, you should be able to:
- use the choice function to generate a random string from `string.ascii_letters`,  `string.digits` + `string.punctuation` characters in Python 3
- generate a secure random string, useful to create random passwords

## Generating a Random String With Upper Case, Lower Case, Digits and Punctuation

The `string` module comes with a [nice set of constants](https://docs.python.org/3/library/string.html#string-constants) that we can combine with the `random` module to create our random string.

### Using Python's `string.ascii_letters + string.digits + string.punctuation` Characters

By concatenating `string.ascii_letters` + `string.digits` + `string.punctuation`, we will have our pool of characters that we can pick at random using the  [`random.choices()`](https://docs.python.org/3/library/random.html#random.choices) method. This method returns a k sized list of elements chosen from the population with replacement. In our case, `k` will be the size of our random string.

```python
>>> import string

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

>>> string.digits
'0123456789'

>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

>>> all_chars = string.ascii_letters + string.digits + string.punctuation

>>> import random

>>> ''.join(random.choices(all_chars, k=10))
'4`<WJ."=$r'
```

This works well, but there is one limitation: `random.choices` is available only on Python 3.6+. To make that work in older versions, you'll need to call the `random.choice` function and iterate over k times.

```python
>>> ''.join((random.choice(all_chars) for _ in range(10)))
'd&6Bx5PX(R'
```

## How to Generate a Cryptographically Secure Random String

The previous method works well if all we want is a random string for simple use cases. If what we want is a random string to be used as a password, then we need a more secure method. The reason is that the random module does not use a secure pseudo-number generator.

As an alternative we must resort to the Operating System’s pseudo-random number generator. The good news is that Python can access that using the `random.SystemRandom` class, which [ensures that sequences are not reproducible](https://stackoverflow.com/a/23728630/14386821).

We can re-use the previous example and change just one minor detail.

From: `random.choices` to `random.SystemRandom()`

```python
>>> all_chars = string.ascii_letters + string.digits + string.punctuation

>>> import random

>>> ''.join(random.SystemRandom().choices(all_chars, k=10))
'T$WoW.sdQc'
```

## Conclusion

In this post we saw 2 different ways of creating random strings Python. I hope you find it useful.

Other posts you may like:

- [Design Patterns That Make Sense in Python: Simple Factory](#https://miguendes.me/design-patterns-that-make-sense-in-python-simple-factory)
- [How to Pass Multiple Arguments to a map Function in Python](https://miguendes.me/how-to-pass-multiple-arguments-to-a-map-function-in-python)
- [73 Examples to Help You Master Python's f-strings](https://miguendes.me/73-examples-to-help-you-master-pythons-f-strings)
- [3 Ways to Unit Test REST APIs in Python](https://miguendes.me/3-ways-to-test-api-client-applications-in-python)
- [Everything You Need to Know About Python's Namedtuples](https://miguendes.me/everything-you-need-to-know-about-pythons-namedtuples)
- [The Best Way to Compare Two Dictionaries in Python](https://miguendes.me/the-best-way-to-compare-two-dictionaries-in-python)
- [5 Hidden Python Features You Probably Never Heard Of](https://miguendes.me/5-hidden-python-features-you-probably-never-heard-of)


See you next time!

This post was originally published at [https://miguendes.me](https://miguendes.me/how-to-implement-a-random-string-generator-with-python)


