Множество букв в питоне

List of Alphabets Python

Here we develop a program to print the list of alphabets in Python. An alphabet is a set of letters or symbols in a fixed order used to represent the basic set of speech sounds of a language, especially the set of letters from A to Z. We will develop a Python program to initialize the list with the English alphabets a-z using various methods.

Alphabet list Python

This python program using the For Loop to print the list of uppercase and lowercase alphabets. The most general method that comes to our mind is using the brute force method of running a loop till 26 and incrementing it while appending the letters in the list. The ord() method is used to find the Unicode value of a character passed as its argument. The chr() method returns a character (a string) from an integer (represents Unicode code point of the character).

# Python program to print list of alphabets # initializing empty list list_upper = [] list_lower = [] upper = 'A' for c in range(0, 26): list_upper.append(upper) upper = chr(ord(upper) + 1) lower = 'a' for c in range(0, 26): list_lower.append(lower) lower = chr(ord(lower) + 1) # print uppercase alphabets print('Uppercase Alphabets: ', list_upper) # print lowercase alphabets print('Lowercase Alphabets: ', list_lower)

Uppercase 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’]Lowercase 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’]

Читайте также:  Php fpm log php errors

Python Alphabet List

This method is similar to the above method, but rather a shorthand method. In this program, we use the list comprehension technique.

# Python program to print list of alphabets # using list comprehension list_upper = [chr(i) for i in range(ord('A'), ord('Z') + 1)] list_lower = [chr(i) for i in range(ord('a'), ord('z') + 1)] # print uppercase alphabets print('Uppercase Alphabets: ', list_upper) # print lowercase alphabets print('Lowercase Alphabets: ', list_lower)

Uppercase 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’]Lowercase 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’]

Alphabet list in Python

The map() function applies a given function to each item of an iterable (list, tuple, etc.) and returns a list of the results. It typecasts the numbers in a range to a particular data type, char in this case, and assigns to the list.

# Python program to print list of alphabets # using map() list_upper = list(map(chr, range(65, 91))) list_lower = list(map(chr, range(97, 123))) # print uppercase alphabets print('Uppercase Alphabets: ', list_upper) # print lowercase alphabets print('Lowercase Alphabets: ', list_lower)

Uppercase 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’]Lowercase 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’]

This python program also performs the same task but in a different way. In this program, we are using the built-in function to print the list of alphabets. The string.ascii_uppercase method returns all uppercase alphabets and string.ascii_lowercase method returns all lowercase alphabets.

# Python program to print list of alphabets import string #importing string function # using string list_upper = list(string.ascii_uppercase) list_lower = list(string.ascii_lowercase) # print uppercase alphabets print('Uppercase Alphabets: ', list_upper) # print lowercase alphabets print('Lowercase Alphabets: ', list_lower)

Uppercase 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’]Lowercase 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’]

Order List Alphabetically

In the previous program, we used string.ascii_uppercase and string.ascii_lowercase but in this program, we are using string.ascii_letters method. This method returns all lowercase and uppercase alphabets as a single string.

# Python program to print list of alphabets import string #importing string function # using string list_alpha = list(string.ascii_letters) # print alphabets print('Alphabets: ', list_alpha)

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’]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

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

Источник

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