How to generate a random number/string in Python

The standard way to generate a random number is to use the random (opens new window) module. The random() function will return a floating point number from 0 to 1 (but not including 1). In addition, there's also a choice() function that will randomly select an item from a list. choice() can be used to select from a pre-specified list of characters or numbers.

Here's an example of how to generate a random number using random() and choice():

Copy code
      
    


And here's an example of how to use choice() to generate a random string:

Copy code
      
    

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.