Alphabet range in Python
>>> 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'
Python: how to print range a-z?
>>> import string
>>> string.ascii_lowercase[:14]
'abcdefghijklmn'
>>> string.ascii_lowercase[:14:2]
'acegikm'
To do the urls, you could use something like this
[i + j for i, j in zip(list_of_urls, string.ascii_lowercase[:14])]
Is There an Already Made Alphabet List In Python?
The string
module provides several (English-centric) values:
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
You'll have to create your own vowel/consonant lists, as well as lists for other languages.
Given how short the list of vowels is, vowels and consonants aren't too painful:
>>> vowels = set("aeiou")
>>> set(string.ascii_lowercase).difference(vowels)
{'b', 'f', 'v', 'q', 's', 'w', 'y', 'l', 'g', 'j', 'z', 'c', 'h', 'p', 'x', 'd', 'm', 'n', 't', 'k', 'r'}
How to check if words starts with within the alphabet range
Just use startswith()
like so:
quote = input("Enter your name : ")
allowed = 'abcdefg'
if any(quote.startswith(x) for x in allowed):
print('Success!')
Alternatively and to make it more flexible you can use this:
import string
start = 'a'
end = 'g'
letters = string.ascii_lowercase
allowed = letters[letters.index(start):letters.index(end)+1] # abcdefg
quote = input("Enter your name : ")
while not any(quote.startswith(x) for x in allowed):
quote = input("The name is not valid! Please enter another name: ")
print('Success!')
Alphabet range with num and item from list
Here is one way.
from string import ascii_uppercase
lst = ['cat', 'cow', 'dog']
for num, (letter, item) in enumerate(zip(ascii_uppercase, lst), 1):
print('{0}{1} {2}'.format(letter, num, item))
# A1 cat
# B2 cow
# C3 dog
Explanation
- Use
enumerate
for a numeric counter with optional start value of 1. - Use
string.ascii_uppercase
to loop upper case alphabet. zip
and loop through, usingstr.format
to format your output.
How to use the `match` keyword to match a range?
You would need to define a class which has an instance that can compare equal to "b"
, then find a way to refer to such an object using a dotted name. Something like
class CharRange:
def __init__(self, start, stop):
self.start = start
self.stop = stop
def __eq__(self, other):
if isinstance(other, str):
return self.start <= other <= self.stop
else:
return self.start == other.start and self.stop == other.stop
class CharRanges:
lowercase = CharRange("a", "z")
match "b":
case "a":
print("a")
case CharRanges.lowercase:
print("alphabet")
You can't use case CharRange("a", "z")
because that's a class pattern, which (without going into how class patterns work) won't help you here, and you can't use
x = CharRange("a", "z")
match "b":
case x:
...
because x
is a capture pattern, matching anything and binding it to the name x
.
Related Topics
How to Get Python's Elementtree to Pretty Print to an Xml File
How to Set the Text/Value/Content of an 'Entry' Widget Using a Button in Tkinter
Sum of List of Lists; Returns Sum List
Scrapy - Reactor Not Restartable
Intercepting Stdout of a Subprocess While It Is Running
Improper Use of _New_ to Generate Class Instances
Why Do Some Built-In Python Functions Only Have Pass
How to Change the Python Version in Visual Studio Code
How to Find a Particular JSON Value by Key
Differencebetween Len() and Sys.Getsizeof() Methods in Python