16

Is there a built-in method / module in Python to generate letters such as the built-in constant LETTERS or letters constant in R?

The R built-in constant works as letters[n] where if n = 1:26 the lower-case letters of the alphabet are produced.

Thanks.

John
  • 32,659
  • 27
  • 74
  • 102

4 Answers4

24

It's called string.ascii_lowercase.

If you wanted to pick n many random lower case letters, then:

from string import ascii_lowercase
from random import choice

letters = [choice(ascii_lowercase) for _ in range(5)]

If you wanted it as a string, rather than a list then use str.join:

letters = ''.join([choice(ascii_lowercase) for _ in range(5)])
Jon Clements
  • 124,071
  • 31
  • 219
  • 256
  • I had the link on the clipboard while composing my unposted answer :) –  Oct 11 '12 at 08:52
  • 1
    @Tichodroma, @jon-clements - thanks. This is nice. I can easily pass a sequence to it such as `string.ascii_letters[1:5]`. However, is there a way to generate a series of random letters that are evaluated from an expression producing random integers between 0 and 26? When I try with an array result from `string.ascii_letters[np.random.randint(0,26,10)]` it tells me I can only have one element converted to an index. – John Oct 11 '12 at 09:10
  • @John that's a new question, and is about subsetting in python. Python is not R! – Spacedman Oct 11 '12 at 09:54
  • 3
    @ John -- use random.sample(string.ascii_letters,5) – root Oct 11 '12 at 09:59
  • 2
    More generally, Python tends to use "list comprehension" when R is magically vectorised. – Spacedman Oct 11 '12 at 10:00
  • @Spacedman - ah-ha! `[string.ascii_letters[i] for i in np.random.random_integers(0,26,5)]` – John May 31 '14 at 03:59
  • 1
    @John I'd go for `[random.choice(string.ascii_letters) for _ in range(5)]` but I suppose if you want to use `numpy` to generate the numbers then index into the list that's fine :) – Jon Clements May 31 '14 at 12:19
  • @John I've updated the answer to reflect examples of how to produce either a list or a string of *n* random lower case letters – Jon Clements May 31 '14 at 12:43
  • @JonClements - that is a lot cleaner. – John Jun 01 '14 at 01:17
13

You can use map as in the following:

>>> map(chr, range(65, 91))
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
>>> map(chr, range(97, 123))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> a = map(chr, range(65, 70))
>>> a
['A', 'B', 'C', 'D', 'E']
A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450
5

With list comprehensions and reference from the above, there is another method:

>>> [chr(x) for x in range(97, 123)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
jl81
  • 85
  • 1
  • 8
1

Yet another way to do it which will give you directly a string:

>>> bytearray(range(97,123)).decode("utf-8")
u'abcdefghijklmnopqrstuvwxyz'

(it works with both python2 and python3, of course the u prefix won't be visible if it's python 3)

You can obviously tranform that string into a list like in other answers if that is what you prefer, for instance with:

>>> [x for x in bytearray(range(97,123)).decode("utf-8")]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

It is also very easy to change it to pick n random letters (repetition allowed):

>>> import random
>>> n = 10
>>> bytearray(random.randint(97, 122) for x in range(n)).decode('utf-8')
'qdtdlrqidx'

Or without repetition:

>>> import random
>>> n = 10
>>> bytearray(random.sample(range(97, 123),n)).decode('utf-8')
'yjaifemwbr'
kriss
  • 21,366
  • 15
  • 89
  • 109