Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabet range in Python

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) 
like image 849
Alexa Elis Avatar asked Apr 17 '13 13:04

Alexa Elis


People also ask

Can you do a range with letters in Python?

To produce a range of letters (characters) in Python, you have to write a custom function that: Takes start and end characters as input. Converts start and end to numbers using ord() function. Generates a range of numbers between start and end.

Is there an alphabet function in Python?

The isalpha() function is a built-in function used for string handling in python, which checks if the single input character is an alphabet or if all the characters in the input string are alphabets.

How many alphabets are there in Python?

The Python module string is readily available and contains pre-defined constant values that we can use for this problem. The constant string. ascii_lowercase contains all 26 lowercase characters in string format.


1 Answers

>>> 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' 
like image 148
jamylak Avatar answered Oct 10 '22 23:10

jamylak