Sometimes, we want to generate a random string generation with upper case letters and digits with Python.
In this article, we’ll look at how to generate a random string generation with upper case letters and digits with Python.
How to generate a random string generation with upper case letters and digits with Python?
To generate a random string generation with upper case letters and digits with Python, we can use the random.choices
method.
For instance, we write
s = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
to call random.choices
with the list of characters we want to choose from that we get from
string.ascii_uppercase + string.digits
And then we set k
to N
characters to return N
characters in the generated string.
We join the returned characters list into a string with join
.
Conclusion
To generate a random string generation with upper case letters and digits with Python, we can use the random.choices
method.