730

How do I create a list of alphabet characters, without doing it manually like this?

['a', 'b', 'c', 'd', ..., 'z']
3
  • 2
    @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) Commented Feb 11, 2018 at 10:06
  • dupe of stackoverflow.com/questions/14927114/… (which itself also seems to be a dupe) Commented Jul 14, 2018 at 15:27
  • 3
    @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) Commented Jan 28, 2019 at 2:59

12 Answers 12

1309
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> 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']

Alternatively, using range:

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

Or equivalently:

>>> 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)
....
DATA
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    whitespace = ' \t\n\r\x0b\x0c'
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. How do I make it to reverse order?
string.ascii_lowercase[::-1]
@haavee Actually no in Python 3 map doesn't return a list
I wonder if there's a way to do the same for a specific locale, i.e. get the spanish, turkish, etc. alphabets
|
175
[chr(i) for i in range(ord('a'),ord('z')+1)]

7 Comments

I got: [chr(alpha+97) for alpha in range(0,27)] but this is much more intuitive. Doesn't require remembering that ascii of a is 97
@MoeChughtai I don't understand how is this more succinct than string.ascii_lowercase
Also: chrange = lambda s: "".join(map(chr, range(*map(ord, s))) + [c[1]]). Usage: >>> chrange("az") -> 'abcdefghijklmnopqrstuvwxyz'. For a list, just remove "".join( )
@jamylak Maybe MoeChughtai meant that this answer really doesn't drown the solution in lengthy explanations.
@Fornost import string is a lengthy explanation?
|
67

In Python 2.7 and 3 you can use this:

import string
string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

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

3 Comments

In Python 3, use string.ascii_lowercase. But this returns a string. In case you need a list, I think, Bg1850 is a neat solution
As the top answer mentions, string.ascii_letters, string.ascii_lowercase, string.ascii_uppercase all work in python 3+.
@peterb list(string.ascii_lowercase)
23

Here is a simple letter-range implementation:

Code

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

Demo

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

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

2 Comments

Very nice! Also works with non-Latin characters. I just tried.
for ord_ in range(ord(start.lower()), ord(stop.lower()) + 1, step): +1 includes last symbol into list
17

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

import string
list(string.ascii_lowercase[0:10])

Comments

8

This is the easiest way I can figure out:

#!/usr/bin/python3
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

1 Comment

Neat! As a compact one liner [f"{n:c}" for n in range(97, 97+10)]
4

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=" ")
    print()

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

upperCaseAlphabets();
lowerCaseAlphabets();

Comments

3

you can do without import

  list = [chr(item) for item in range(ord("a"), ord("z") + 1)]

Comments

2

Here is how I implemented my custom function for letters range generation based on string.ascii_letters:

from string import ascii_letters


def range_alpha(start_letter, end_letter):
  return ascii_letters[
    ascii_letters.index(start_letter):ascii_letters.index(end_letter) + 1
  ]

print(range_alpha('a', 'z'))
print(range_alpha('A', 'Z'))
print(range_alpha('a', 'Z'))

Comments

0

Although this is an old question, I'll give an answer which is quite flexible. If you have PyICU installed, this can be easily leveraged for this task:

from icu import UnicodeSet
lset = UnicodeSet('[a-z]')
print(list(lset))
# ['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']
ulset = UnicodeSet('[a-zA-Z]')
print(list(ulset))
# ['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', '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']

But you can also use any UnicodeSet patterns in the same way:

uset2 = UnicodeSet('[[:Ll:]&[:Latin:]]')

This pattern is an intersection between two sets. The intersection between all lowercase characters and all Latin characters, i.e. all the lowercase Latin characters.

Comments

0

If you want upper/lower case range and also want alternate or with steps then you try below code.

def letter_range(start, stop="{", step=1):
    """Yield a range of upper/lowercase letters."""
    if start.isupper():
        if stop == "{":
            stop = "["
        else:
            stop = stop.upper()
        ascii_uppercase = [chr(item) for item in range(ord(start), ord(stop), step)]
        print(ascii_uppercase)

    elif start.islower():
        ascii_lowercase = [chr(item) for item in range(ord(start), ord(stop), step)]
        print(ascii_lowercase)
    else:
        print("Check your input")


letter_range("a", "f", 2)
letter_range("A", "f",)
['a', 'c', 'e']
['A', 'B', 'C', 'D', 'E']

Comments

-2

Using list comprehension and the chr() function:

letter_list = [chr(x) for x in range(97, 122)]

Will return the required list from 'a' to 'z'.

1 Comment

Somebody else already posted this answer (actually a better version). When you answer a really old question with lots of answers, make sure someone else hasn't added the same thing already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.