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>.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
random.sample
is an alternative choice. The difference, as can be found in the python.org documentation, is thatrandom.sample
samples without replacement. Thus,random.sample(string.letters, 53)
would result in aValueError
. Then if you wanted to generate your random string of eight digits and fifteen characters, you would writeEDIT: 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:
Obviously for uppercase only you can just flip that around so the slice is [26:] instead of the other way around.
Here's a simpler version:
See an example - Recipe 59873: Random Password Generation .
Building on the recipe, here is a solution to your question :