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)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
>>> 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!"#$%&'()*+,-./:;<=><a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ad92ed">[email protected]</a>[\]^_`{|}~ tnrx0bx0c'
punctuation = '!"#$%&'()*+,-./:;<=><a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="615e21">[email protected]</a>[\]^_`{|}~'
whitespace = ' tnrx0bx0c'
Method 2
[chr(i) for i in range(ord('a'),ord('z')+1)]
Method 3
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
Method 4
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']
Method 5
If you are looking to an equivalent of letters[1:10] from R, you can use:
import string list(string.ascii_lowercase[0:10])
Method 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
Method 7
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();
Method 8
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'))
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0