Random string generation with upper case letters and digits

前端 未结 30 3594
逝去的感伤
逝去的感伤 2020-11-22 02:51

I want to generate a string of size N.

It should be made up of numbers and uppercase English letters such as:

  • 6U1S75
  • 4Z4UKK
  • U911K4
30条回答
  •  执笔经年
    2020-11-22 03:03

    import random
    q=2
    o=1
    list  =[r'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','s','0','1','2','3','4','5','6','7','8','9','0']
    while(q>o):
        print("")
    
        for i in range(1,128):
            x=random.choice(list)
            print(x,end="")
    

    Here length of string can be changed in for loop i.e for i in range(1,length) It is simple algorithm which is easy to understand. it uses list so you can discard characters that you do not need.

提交回复
热议问题