Sequence of letters in Python

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.

Answers


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)])

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']

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']

Need Your Help

Parse versionCode from android apk files

android packaging android-manifest apk

I know .apk files are .zip files, but AndroidManifest.xml looks binary and certainly grep or hex editors haven't been successful extracting the versionCode.

Why do people use tarballs?

linux package archive

As a primarily Windows developer, perhaps I'm missing something cultural in the Linux community, but it has always confused me.