Skip to main content
Title should not be a question but a description of what the code does.
Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68

Python. How Generate random string to concatenate generated values properly?match a specific pattern

Source Link

Python. How to concatenate generated values properly?

import random
import string


def random_char(y):
    return ''.join(random.choice(string.ascii_uppercase) for x in range(y))


p1 = str(random.randrange(1, 99))
p2 = str(random_char(5))
p3 = str(random.randrange(1, 9))
p4 = str(random_char(2))
p5 = str(random.randrange(1, 9))
p6 = str(random_char(1))  # Y
p7 = str(random.randrange(1, 9))  # 7
p8 = str(random_char(3))  # AUS
result = p1 + p2+p3+p4+p5+p6+p7+p8
print(result)

I generate a specific code like "31SPZVG2CZ2R8WFU" How can i do it in a more elegant way?