586

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)
Matthias Braun
  • 28,341
  • 18
  • 134
  • 157
Alexa Elis
  • 6,449
  • 6
  • 16
  • 11
  • 2
    seems some answer might need update for python 3? – Charlie Parker Jun 15 '17 at 20:07
  • 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) – jamylak Feb 11 '18 at 10:06
  • dupe of https://stackoverflow.com/questions/14927114/is-it-possible-to-make-a-letter-range-in-python (which itself also seems to be a dupe) – hkBst Jul 14 '18 at 15:27
  • 2
    @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) – jamylak Jan 28 '19 at 02:59

8 Answers8

1070
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

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
....
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'
jamylak
  • 120,885
  • 29
  • 225
  • 225
146
[chr(i) for i in range(ord('a'),ord('z')+1)]
Bg1850
  • 2,771
  • 1
  • 14
  • 29
  • 1
    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 – peterb Aug 25 '16 at 05:45
  • 7
    @MoeChughtai I don't understand how is this more succinct than `string.ascii_lowercase` – jamylak Oct 19 '16 at 07:28
  • Also: `chrange = lambda s: "".join(map(chr, range(*map(ord, s))) + [c[1]])`. Usage: `>>> chrange("az") -> 'abcdefghijklmnopqrstuvwxyz'`. For a list, just remove `"".join( )` – Braden Best Aug 26 '18 at 02:33
  • @jamylak Maybe MoeChughtai meant that this answer really doesn't drown the solution in lengthy explanations. – Fornost Jan 27 '19 at 09:58
  • 2
    @Fornost `import string` is a lengthy explanation? – jamylak Jan 27 '19 at 22:08
  • Your answer had many more words than these two, but is more helpful. Helpfulness beats concision, according to the upvotes your answer received. – Fornost Jan 28 '19 at 19:18
  • @jamylak because there is not string module involved. – HaseeB Mir May 21 '19 at 23:52
57

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

Luc
  • 4,264
  • 2
  • 43
  • 44
Trinh Nguyen
  • 1,295
  • 1
  • 13
  • 21
17

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']
pylang
  • 34,585
  • 11
  • 114
  • 108
14

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

import string
list(string.ascii_lowercase[0:10])
Tiago Martins Peres
  • 12,598
  • 15
  • 77
  • 116
Qaswed
  • 3,179
  • 5
  • 24
  • 41
6

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

Tiago Martins Peres
  • 12,598
  • 15
  • 77
  • 116
RicarHincapie
  • 1,839
  • 12
  • 19
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();
Lakshmikandan
  • 3,935
  • 2
  • 27
  • 35
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'))