Generate a random letter in Python
350,365
Solution 1
Simple:
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> import random
>>> random.choice(string.ascii_letters)
'j'
string.ascii_letters
returns a string containing the lower case and upper case letters according to the current locale.
random.choice
returns a single, random element from a sequence.
Solution 2
>>> import random
>>> import string
>>> random.choice(string.ascii_letters)
'g'
Solution 3
>>>def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
>>>print (random_char(5))
>>>fxkea
to generate y number of random characters
Solution 4
>>> import random
>>> import string
>>> random.choice(string.ascii_lowercase)
'b'
Solution 5
You can use this to get one or more random letter(s)
import random
import string
random.seed(10)
letters = string.ascii_lowercase
rand_letters = random.choices(letters,k=5) # where k is the number of required rand_letters
print(rand_letters)
['o', 'l', 'p', 'f', 'v']
Related videos on Youtube
Author by
Kudu
Updated on January 05, 2022Comments
-
Kudu almost 2 years
Is there a way to generate random letters in Python (like random.randint but for letters)? The range functionality of random.randint would be nice but having a generator that just outputs a random letter would be better than nothing.
-
Taylor Leese over 13 yearsIt's actually string.ascii_lowercase or string.ascii_uppercase.
-
Taylor Leese over 13 yearsThis can be lower or uppercase. Not sure if that is what is needed.
-
Rich over 13 yearsThat depends on what alphabet we're talking about ;-)
-
Hooked over 11 yearsWelcome to Stack Overflow! While this is a good answer it is identical to the already posted and accepted answer by @MarkRushakoff, answered two years ago. Please review the answers before you post so we can keep the signal to noise ratio down.
-
Devin almost 10 yearsOften, I need a string of randoms, here's that (after
from string import ascii_letters, digits
andfrom random import choice
):''.join([choice(ascii_letters + digits) for i in range(32)])
-
Wilson Canda over 9 years@joaquin string.letters is present in python 2.7.7.
-
Dannid over 7 yearsalso:
''.join(random.sample(string.ascii_lowercase,5))
-
Droppy over 6 years@Dannid Doesn't
random.sample()
return a unique set of values from the input, which is not the same asrandom.choice()
? -
Dannid over 6 yearsYes, though if you're choosing just one letter that doesn't make a difference Furthermore, you may want 5 unique letters - the OP didn't specify, and both
random.choice
andrandom.randint
return a single value. You can also usenumpy.random.choice
to give a unique set if you add replace=False, like so:numpy.random.choice(string.ascii_lowercase, size=5, replace=False)
-
Uvuvwevwevwe over 5 years@TaylorLeese, as I have known that there are three options including
ascii_letters
,ascii_uppercase
, andascii_lowercase
. -
grepit about 5 yearsyour answer needs improvement and is not very functional.
-
Azat Ibrakov about 5 yearsin your case you can simply do
print(chr(96 + letter))
, noif
-elif
hell is needed -
Sal-laS over 4 years@NigelRen what is the distribution of
random.choices
? -
zhongxiao37 over 4 yearsshouldn't it be
chr(random.randrange(97, 97 + 26 - 1))
? -
Kieran Moynihan over 4 years@zhongxiao37 Really, it should be
chr(random.randrange(97, 97 + 26)
.random.randrange()
is exclusive on its upper bound, meaning that in order to get the whole range of characters97 - 122
, the argument passed must be123
. -
zhongxiao37 over 4 years@KieranMoynihan Thanks for sharing. I double checked that and you're right. Now I see why
97 + 26
is used. -
Qaswed about 4 years@SalmanLashkarara
help(random.choices)
statesIf the relative weights or cumulative weights are not specified, the selections are made with equal probability.
This would mean, that the distribution is the discrete uniform distribution (en.wikipedia.org/wiki/Discrete_uniform_distribution). -
Qaswed about 4 years1. With Python3, it would be
string.ascii_letters
2. You can save the list comprehension by usingkeylist = random.choices(base_str(), k=KEY_LEN)
3. Why havingbase_str
as a function and not abase_str = string.ascii_letters+string.digits
? -
Mattwmaster58 over 3 yearsWhy are you iterating between 64 and 90? Why not just go from 0 to 26, and offset a? This answer is quite poor, the output hardly random:
@AAADCCEHFGJLDJF@EHFMHKWUR
-
colappse over 2 yearsOh, and y is not defined. Be careful about that.
-
Saad over 2 years@colappse y is the argument passed to the function
-
colappse over 2 years@saad okay I understand you
-
Danwand N S about 2 yearsThis is
if-else
hell, It is not a good practice