Home Python Producing a random string – Yasoob Khalid

Producing a random string – Yasoob Khalid

Producing a random string – Yasoob Khalid


Okay, so, most of us have no idea tips on how to generate random strings which embrace letters and digits. This may be actually helpful for producing a password (or, you realize, stuff to assist you in your plan for world domination). So how do we generate a random string? Have you ever ever heard of the string module out there in python? Likelihood is, you haven’t. So what does this module present us? Okay right here you go lets perceive this module by instance:

# first we've to import random module as this
# supplies the spine for our random string
# generator after which we've to import the string
# module.

>>> import random
>>> import string

# now lets see what this string module present us.
# I wont be going into depth as a result of the python
# documentation supplies ample info.
# so lets generate a random string with 32 characters.

>>> random = ''.be a part of([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])
>>> print random
>>> print len(random)

One other instance with a perform:

>>> import string
>>> import random
>>> def random_generator(dimension=6, chars=string.ascii_uppercase + string.digits):
...    return ''.be a part of(random.alternative(chars) for x in vary(dimension))
>>> random_generator()
>>> random_generator(3, "6793YUIO")

So thats it for now. If you wish to additional discover the string module then go to the official documentation.



Please enter your comment!
Please enter your name here