Generating random text strings of a given pattern

前端 未结 5 723
野性不改
野性不改 2020-12-08 07:40

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 st

5条回答
  •  北海茫月
    2020-12-08 08:21

    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.

提交回复
热议问题