Строка всех букв python

Python Alphabet | Ways to Initialize a List of the Alphabet

Python Alphabet

Sometimes while working with the alphabet in python, to make our task easy, we want to initialize a list containing all the alphabets. If we do not know how to do it using python, we will manually type all the alphabets, which will be quite a time taking. In this article, we will learn many different ways of initializing a list containing the alphabets in uppercase, lowercase, in both uppercase and lowercase. We will also learn how to do various operations on alphabets.

Python Alphabets are the same as the lower-level C programming language. They have a unique ASCII value attached to them. With the help of these ascii values, you can convert them into characters and numbers. In this post, we’ll go through all the ways to create a list of alphabets.

In every programming language, including python, every alphabet has a unique ASCII value. We can convert those ASCII values into alphabets using chr and ord functions.

Читайте также:  METANIT.COM

Generating a list of Alphabets in Python

There are many ways to initialize a list containing alphabets, and we will start with the naïve way.

General Approach for Python Alphabet

The ASCII value of A-Z lies in the range of 65-90, and the for a-z, the value is in the range 97 – 122. What we will do is that we will run the loop in this range and using chr(), we will convert these ASCII values into alphabets.

# initialize an empty list that will contain all the capital # alphabets alphabets_in_capital=[] for i in range(65,91): alphabets_in_capital.append(chr(i)) print(alphabets_in_capital)
['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']

python alphabet

# initialize an empty list that will contain all the lowercase alphabets alphabets_in_lowercase=[] for i in range(97,123): alphabets_in_lowercase.append(chr(i)) print(alphabets_in_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']

Python Alphabet using list comprehension

We can initialize the variable with either ‘a’ or ‘A’ and keep incrementing the ASCII value of the variable. We will run the loop 26 times as there are 26 alphabets in the English language.

var='a' alphabets=[] # starting from the ASCII value of 'a' and keep increasing the # value by i. alphabets=[(chr(ord(var)+i)) for i in range(26)] print(alphabets)
['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']
var='A' alphabets=[] alphabets=[(chr(ord(var)+i)) for i in range(26)] print(alphabets)
['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']

Python Alphabet using map function

We can also use the map function to produce the list of alphabets, let’s see how.

# make a list of numbers from 97-123 and then map(convert) it into # characters. alphabet = list(map(chr, range(97, 123))) print(alphabet)
['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']
alphabets = list(map(chr, range(ord('A'), ord('Z')+1))) print(alphabets)

Importing the String Module

We can also import the string module, and use its functions to make a list of alphabets without any hassle.

import string lowercase_alphabets=list(string.ascii_lowercase) print(lowercase_alphabets) uppercase_alphabets=list(string.ascii_uppercase) print(uppercase_alphabets) alphabets=list(string.ascii_letters) print(alphabets)
['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'] ['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']

How to check if the character is Alphabet or not in Python

If we want to check if the character is an alphabet or not, we can use the if condition or the built-in function. Let’s see how.

Using If Condition

variable='A' if (variable>='a' and variable<='z') or (variable>='A' and variable<='Z'): print("isalphabet") else: print("not a alphabet")

Using built-in function

We can also use the isalpha() method to check whether the character is an alphabet or not.

variable='g' print(variable.isalpha()) print('1'.isalpha())

How to Convert alphabet into ASCII value

Let us see how we can convert alphabets into their respective ASCII values.

var='G' # using ord() function print(ord(var))

Must Read:

Conclusion

Generally in dynamic programming or while making any application, we need to initialize a Python list containing the alphabets. There are a variety of ways using we can make a list containing all the alphabets like using the string module or by using the ASCII values.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Источник

How to create a list of English alphabet letters (from A to Z) with python ?

Examples of how to create a list of English alphabet letters (from A to Z) with python:

List of alphabet letters (lowercase)

To get a list of English alphabet letters with python, a straightforward solution is to use the library called string

import string english_alphabet_string_lowercase = string.ascii_lowercase english_alphabet_string_lowercase 
'abcdefghijklmnopqrstuvwxyz' 

Then to transform the above string to a list, just use the list() function:

english_alphabet_string_lowercase_list = list(english_alphabet_string_lowercase) print( english_alphabet_string_lowercase_list ) 
['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'] 

Note: if you want to upper case a given letter, b for instance:

a solution is to use the method upper():

english_alphabet_string_lowercase_list[1] = english_alphabet_string_lowercase_list[1].upper() print( english_alphabet_string_lowercase_list ) 
['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'] 

List of alphabet letters in (uppercase)

To create a list of alphabet letters in uppercase, we can use a list comprehension:

[l.upper() for l in english_alphabet_string_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'] 

or directly from the library sring:

english_alphabet_string_uppercase= string.ascii_uppercase english_alphabet_string_uppercase 
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 

then create a list using list() method:

english_alphabet_string_uppercase_list = list(english_alphabet_string_uppercase) print( english_alphabet_string_uppercase_list ) 
['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'] 

Note: same to transform a given letter from uppercase to lowercase

english_alphabet_string_uppercase_list[1] B 

List of lowercase and uppercase alphabet letters

alphabet = list(string.ascii_lowercase) + list(string.ascii_uppercase) 
['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'] 

Example of application: create a simple coded message

Create a list of alphabet letters

a1 = list(string.ascii_lowercase) + list(string.ascii_uppercase) print( alphabet ) 
['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'] 

Rotate elements of the previous list

from collections import deque a2 = deque( a1 ) a2.rotate(2) print( list( a2 ) ) 
['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', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'] 

Create a secret dictionary

secret = <> for letter, coded_letter in zip(a1,a2): secret[letter] = coded_letter 

and a function to encode a word:

def encoder_function(letter): return secret[letter] 

Now let's encode the word 'Hello':

''.join( list( map(encoder_function, s) ) ) 

Benjamin

Greetings, I am Ben! I completed my PhD in Atmospheric Science from the University of Lille, France. Subsequently, for 12 years I was employed at NASA as a Research Scientist focusing on Earth remote sensing. Presently, I work with NOAA concentrating on satellite-based Active Fire detection. Python, Machine Learning and Open Science are special areas of interest to me.

Skills

Источник

Оцените статью