0
from string import ascii_uppercase, digits
import string
import random


def generatestr():
    str0 = random.sample(digits + ascii_uppercase,2) + random.sample(ascii_uppercase,1) + random.sample(digits + ascii_uppercase,2)
    str1 = random.sample(digits + ascii_uppercase,5)
    str2 = random.sample(digits + ascii_uppercase,3) + random.sample(ascii_uppercase,1) + random.sample(digits + ascii_uppercase,1)
    str3 = random.sample(digits + ascii_uppercase,5)
    str4 = random.sample(digits + ascii_uppercase,4)
    key = str(str0) + "-" + str(str1) + "-" + str(str2) + "-" + str(str3) + "-" + str(str4) + "Z"
    return ''.join(key)

    

print(generatestr())

['H', 'J', 'U', 'V', '8']-['6', '4', '5', 'Z', '0']-['L', '8', '7', 'D', 'Q']-['9', 'P', 'F', 'T', 'B']-['M', '8', 'G', 'V']Z

Expected output: ABCDE-ABCDE-ABCDE-ABCDE-ABCDZ

5
  • 1
    Why don't you ''.join str0 instead of key? Commented Jan 21, 2023 at 15:56
  • random.sample returns a list which you would need to join in order to get a string. Commented Jan 21, 2023 at 15:58
  • @JohnColeman What do you mean exactly? Apologies as this is a new topic for me Commented Jan 21, 2023 at 16:00
  • 1
    random.sample(digits + ascii_uppercase,2) is a list of 2 characters but ''.join(random.sample(digits + ascii_uppercase,2)) would be a string of length 2 -- which is what you want. Commented Jan 21, 2023 at 16:01
  • return '-'.join(map("".join, (str0, str1, str2, str3, str4)) Commented Jan 21, 2023 at 19:28

5 Answers 5

1

Random.sample returns a list. When you use str(str0) then you change a list to a string which outputs: ['H', 'J', 'U', 'V', '8']

In order to get expected output, you have to change a line to:

key = "".join(str0) + "-" + "".join(str1) + "-" + "".join(str2) + "-" + "".join(str3) + "-" + "".join(str4) + "Z"
Sign up to request clarification or add additional context in comments.

Comments

0

Solution:

    key = ''.join(str0) + "-" + ''.join(str1) + "-" + ''.join(str2) + "-" + ''.join(str3) + "-" + ''.join(str4) + "Z"
    return key

1 Comment

This answer is exactly identical to an earlier answer by @Jodla. StackOverflow considers this a duplicate.
0

str when used on a collection typically gives a string that looks like an inline representation of the collection, hence

str([1,2,3])

outputs

'[1, 2, 3]'

To give a string representation of a list the way you want it, you want to join the items of the list. my_string.join, as you've already noticed, applies the str function to each item of a list, concatenates all the results, separating them using the value of the string my_string, then returns the result. Hence

''.join([1,2,3])

outputs

'123'

So this is what you want:

def generatestr():
    rands = []
    rands.append(random.sample(digits + ascii_uppercase,2) + random.sample(ascii_uppercase,1) + random.sample(digits + ascii_uppercase,2))
    rands.append(random.sample(digits + ascii_uppercase,5))
    rands.append(random.sample(digits + ascii_uppercase,3) + random.sample(ascii_uppercase,1) + random.sample(digits + ascii_uppercase,1))
    rands.append(random.sample(digits + ascii_uppercase,5))
    rands.append(random.sample(digits + ascii_uppercase,4))
    return '-'.join([''.join(rand_list) for rand_list in rands])

Comments

0

Others have said it: random.sample() returns a list. My approach is to generate a big list, then join together using an empty string:

from string import ascii_uppercase, digits
import random


def generatestr():
    key = (
        random.sample(digits + ascii_uppercase, 2)
        + random.sample(ascii_uppercase, 1)
        + random.sample(digits + ascii_uppercase, 2)
        + ["-"]
        + random.sample(digits + ascii_uppercase, 5)
        + ["-"]
        + random.sample(digits + ascii_uppercase, 3)
        + random.sample(ascii_uppercase, 1)
        + random.sample(digits + ascii_uppercase, 1)
        + ["-"]
        + random.sample(digits + ascii_uppercase, 5)
        + random.sample(digits + ascii_uppercase, 4)
    )
    return "".join(key)


print(generatestr())

Notes

  • Do no need to import string
  • This solution uses black to format the code for reaability

Comments

0
import random
import string
random_string1 = "".join(random.choices(string.ascii_uppercase, k =5))
random_string2 = "".join(random.choices(string.ascii_uppercase, k =5))
random_string3 = "".join(random.choices(string.ascii_uppercase, k =3))
random_string4 = "".join(random.choices(string.ascii_uppercase, k =2))
print(random_string1+"-"+random_string2+"-"+random_string3+"-"+random_string4)

Output:-

ZZUSR-ZUNVA-CFS-HE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.