0
randstring = 'test'.join(random.choices(string.digits, k=2))

I want to generate the text as test1,test3. The specific text is 'test' and along with that I want to generate random numbers. I tried the above statement but result is like '1test3' and not 'test1,test3'.

2 Answers 2

1

You need to break it down to two steps:

  1. Generate k random numbers and concatenate them to the string.
  2. join the strings with a comma.

You seem to have mixed up a bit with what to join, so it should look something like:

import random
import string

k = 2
randstring = ','.join("test" + random.choice(string.digits) for _ in range(k))

Alternatively, using choices as you did:

randstring = ','.join("test" + num for num in random.choices(string.digits, k))
Sign up to request clarification or add additional context in comments.

Comments

0
import random
n = random. randint(10, 99) 
randstring = 'test' + str(n)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.