Python how to include a string with random integer [duplicate]


Python how to include a string with random integer [duplicate]



This question already has an answer here:



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?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




2 Answers
2



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


unitnumbers


unitnumbers = "unit " + str(random.randint(1,999))



This should do that:


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

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?