Welcome folks today in this blog post we will be generating random string
with specific length
and characters
using the string
and random
module in python. All the full source code of application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import random import string def randStr(chars = string.ascii_uppercase + string.digits, N=10): return ''.join(random.choice(chars) for _ in range(N)) # default length(=10) random string print(randStr()) # random string of length 7 print(randStr(N=17)) # random string with characters picked from ascii_lowercase print(randStr(chars=string.ascii_lowercase)) # random string with characters picked from 'abcdef123456' print(randStr(chars='abcdef123456')) |
Now just run the python script by typing the below command as shown below
python app.py
So you will see it will generate random string
with specific length and characters. You can change the number of length of characters as a parameter to function
and also which characters
you want to use