28

I need to generate random text strings of a particular format. Would like some ideas so that I can code it up in Python. The format is <8 digit number><15 character string>.

Belphegor
  • 4,456
  • 11
  • 34
  • 59
Vijay Dev
  • 26,966
  • 21
  • 76
  • 96

5 Answers5

61
#!/usr/bin/python

import random
import string

digits = "".join( [random.choice(string.digits) for i in xrange(8)] )
chars = "".join( [random.choice(string.letters) for i in xrange(15)] )
print digits + chars

EDIT: liked the idea of using random.choice better than randint() so I've updated the code to reflect that.

Note: this assumes lowercase and uppercase characters are desired. If lowercase only then change the second list comprehension to read:

chars = "".join( [random.choice(string.letters[:26]) for i in xrange(15)] )

Obviously for uppercase only you can just flip that around so the slice is [26:] instead of the other way around.

Jay
  • 41,768
  • 14
  • 66
  • 83
  • Nice answer! (I had never even seen random.choice before.) – Claes Mogren Dec 15 '08 at 06:35
  • 1
    Probably more readable to use string.lowercase and string.uppercase than slicing the list. Also the solution only holds if the OP is satisfied with only ASCII characters, if he wants to generate strings from the whole unicode character set the problem becomes much harder. – Björn Lindqvist Jul 19 '10 at 14:19
  • 1
    You can pass a generator expression in as the argument, rather than explicitly write a list comprehension: `digits = "".join(random.choice(string.digits) for i in xrange(8))` – Benjamin Hodgson Jan 07 '13 at 12:05
  • 17
    Great answer. If you're using Python 3, replace `string.letters` with `string.ascii_letters` and `range()` can be used instead of `xrange()`. – Jubbles Dec 06 '14 at 20:46
17

See an example - Recipe 59873: Random Password Generation .

Building on the recipe, here is a solution to your question :

from random import choice
import string

def GenPasswd2(length=8, chars=string.letters + string.digits):
    return ''.join([choice(chars) for i in range(length)])

>>> GenPasswd2(8,string.digits) + GenPasswd2(15,string.ascii_letters)
'28605495YHlCJfMKpRPGyAw'
>>> 
gimel
  • 83,368
  • 10
  • 76
  • 104
12

random.sample is an alternative choice. The difference, as can be found in the python.org documentation, is that random.sample samples without replacement. Thus, random.sample(string.letters, 53) would result in a ValueError. Then if you wanted to generate your random string of eight digits and fifteen characters, you would write

import random, string

digits = ''.join(random.sample(string.digits, 8))
chars = ''.join(random.sample(string.letters, 15))
Matthew Turner
  • 3,564
  • 2
  • 20
  • 21
Devi
  • 5,023
  • 1
  • 34
  • 27
  • 5
    In Python 3, `string.letters` has been replaced by `string.ascii_letters`. See: https://docs.python.org/3.1/library/string.html – Jeff Evans Sep 18 '18 at 19:58
4

Shorter version since python 3.6.2, with random.choices over random.choice which doesn't need a for loop, but instead just pass k, the length of the random string required.

import random
import string
x = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
print(x)

You can also add string.punctuation if you need string with punctuation characters.

Riya John
  • 474
  • 4
  • 14
1

Here's a simpler version:

import random
import string

digits = "".join( [random.choice(string.digits+string.letters) for i in   xrange(10)] )
print digits
jithin
  • 119
  • 1
  • 15