string – Alphabet range in Python

The Question :

483 people think this question is useful

Instead of making a list of alphabet characters like this:

alpha = ['a', 'b', 'c', 'd'.........'z']

is there any way that we can group it to a range or something? For example, for numbers it can be grouped using range():

range(1, 10)

The Question Comments :
  • seems some answer might need update for python 3?
  • @CharlieParker No, from the beginning I made sure my answer would work on Python 3 as well as Python 2 at the same time, because i used string.ascii_lowercase (available on both) and not string.lowercase (only on py2)
  • dupe of… (which itself also seems to be a dupe)
  • @hkBst Seems the difference is that those questions are asking for a subset range of letters, while this one requests the entire alphabet (which makes the answer more specific)

The Answer 1

880 people think this answer is useful
>>> import string
>>> string.ascii_lowercase

If you really need a list:

>>> list(string.ascii_lowercase)
['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']

And to do it with range

>>> list(map(chr, range(97, 123))) #or list(map(chr, range(ord('a'), ord('z')+1)))
['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']

Other helpful string module features:

>>> help(string) # on Python 3
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&amp;\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
    punctuation = '!"#$%&amp;\'()*+,-./:;<=>?@[\\]^_`{|}~'
    whitespace = ' \t\n\r\x0b\x0c'

The Answer 2

123 people think this answer is useful
[chr(i) for i in range(ord('a'),ord('z')+1)]

The Answer 3

39 people think this answer is useful

In Python 2.7 and 3 you can use this:

import string


As @Zaz says: string.lowercase is deprecated and no longer works in Python 3 but string.ascii_lowercase works in both

The Answer 4

11 people think this answer is useful

Here is a simple letter-range implementation:


def letter_range(start, stop="{", step=1):
    """Yield a range of lowercase letters.""" 
    for ord_ in range(ord(start.lower()), ord(stop.lower()), step):
        yield chr(ord_)


list(letter_range("a", "f"))
# ['a', 'b', 'c', 'd', 'e']

list(letter_range("a", "f", step=2))
# ['a', 'c', 'e']

The Answer 5

6 people think this answer is useful

If you are looking to an equivalent of letters[1:10] from R, you can use:

import string

The Answer 6

4 people think this answer is useful

This is the easiest way I can figure out:

for i in range(97, 123):
    print("{:c}".format(i), end='')

So, 97 to 122 are the ASCII number equivalent to ‘a’ to and ‘z’. Notice the lowercase and the need to put 123, since it will not be included).

In print function make sure to set the {:c} (character) format, and, in this case, we want it to print it all together not even letting a new line at the end, so end=''would do the job.

The result is this: abcdefghijklmnopqrstuvwxyz

The Answer 7

2 people think this answer is useful

Print the Upper and Lower case alphabets in python using a built-in range function

def upperCaseAlphabets():
    print("Upper Case Alphabets")
    for i in range(65, 91):
        print(chr(i), end=" ")

def lowerCaseAlphabets():
    print("Lower Case Alphabets")
    for i in range(97, 123):
        print(chr(i), end=" ")


Add a Comment