Python how to include a string with random integer

Question:

I want to make a random police unit generator and the code is like so:

import random
unitnumbers = ('unit' , random.randint(1,999))
print(unitnumbers)

However, rather than printing “unit 63” , it prints (‘unit’, 378)

How could I make it print only the unit and random number?

Asked By: pTinosq

||

Answers:

This should do that:

import random
unitnumbers = 'unit %d' % random.randint(1,999)
print(unitnumbers)
Answered By: lenik

You are making the value of unitnumbers a tuple. You should instead make it a string like that:

unitnumbers = "unit " + str(random.randint(1,999))
Answered By: Yossef Gamal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.