Skip to content

Instantly share code, notes, and snippets.

@esavard
Created September 6, 2012 17:19
Show Gist options
  • Save esavard/3658710 to your computer and use it in GitHub Desktop.
Save esavard/3658710 to your computer and use it in GitHub Desktop.
Generate random string in Python
import string
import random
size = 2500 # or whatever lenght you want your random string to be
allowed = string.ascii_letters # add any other allowed characters here
randomstring = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in xrange(size)])
print randomstring
@ameyanekar
Copy link

Thank you for this.

To help other coders, this is how you add character sets to the allowed variable:
allowed = string.ascii_letters + string.digits

Refer: https://docs.python.org/3/library/string.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment