I am learning Python, so pardon me if this is the very crude approach. I am trying to generate a random string using alphanumeric characters. Following is my code:
#Generating random 6 character string from list 'range'.
def code():
# List of characters [a-zA-Z0-9]
chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9']
code = ''
for i in range(6):
n = random.randint(0,61) # random index to select element.
code = code + chars[n] # 6-character string.
return code
What I am trying to do is pick six random elements from the list of characters and append them.
Is there any better approach?