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']
Share:
350,365

Related videos on Youtube

Kudu
Author by

Kudu

Updated on January 05, 2022

Comments

  • Kudu
    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
    Taylor Leese over 13 years
    It's actually string.ascii_lowercase or string.ascii_uppercase.
  • Taylor Leese
    Taylor Leese over 13 years
    This can be lower or uppercase. Not sure if that is what is needed.
  • Rich
    Rich over 13 years
    That depends on what alphabet we're talking about ;-)
  • Hooked
    Hooked over 11 years
    Welcome 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
    Devin almost 10 years
    Often, I need a string of randoms, here's that (after from string import ascii_letters, digits and from random import choice): ''.join([choice(ascii_letters + digits) for i in range(32)])
  • Wilson Canda
    Wilson Canda over 9 years
    @joaquin string.letters is present in python 2.7.7.
  • Dannid
    Dannid over 7 years
    also: ''.join(random.sample(string.ascii_lowercase,5))
  • Droppy
    Droppy over 6 years
    @Dannid Doesn't random.sample() return a unique set of values from the input, which is not the same as random.choice()?
  • Dannid
    Dannid over 6 years
    Yes, 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 and random.randint return a single value. You can also use numpy.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
    Uvuvwevwevwe over 5 years
    @TaylorLeese, as I have known that there are three options including ascii_letters, ascii_uppercase, and ascii_lowercase.
  • grepit
    grepit about 5 years
    your answer needs improvement and is not very functional.
  • Azat Ibrakov
    Azat Ibrakov about 5 years
    in your case you can simply do print(chr(96 + letter)), no if-elif hell is needed
  • Sal-laS
    Sal-laS over 4 years
    @NigelRen what is the distribution of random.choices ?
  • zhongxiao37
    zhongxiao37 over 4 years
    shouldn't it be chr(random.randrange(97, 97 + 26 - 1))?
  • Kieran Moynihan
    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 characters 97 - 122, the argument passed must be 123.
  • zhongxiao37
    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
    Qaswed about 4 years
    @SalmanLashkarara help(random.choices) states If 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
    Qaswed about 4 years
    1. With Python3, it would be string.ascii_letters 2. You can save the list comprehension by using keylist = random.choices(base_str(), k=KEY_LEN) 3. Why having base_str as a function and not a base_str = string.ascii_letters+string.digits?
  • Mattwmaster58
    Mattwmaster58 over 3 years
    Why 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
    colappse over 2 years
    Oh, and y is not defined. Be careful about that.
  • Saad
    Saad over 2 years
    @colappse y is the argument passed to the function
  • colappse
    colappse over 2 years
    @saad okay I understand you
  • Danwand N S
    Danwand N S about 2 years
    This is if-else hell, It is not a good practice