English alphabet python string

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

Источник

How to Make a List of the Alphabet in Python

How to Make a List of the Alphabet in Python Cover Image

In this tutorial, you’ll learn how to use Python to make a list of the entire alphabet. This can be quite useful when you’re working on interview assignments or in programming competitions. You’ll learn how to use the string module in order to generate a list of either and both the entire lower and upper case of the ASCII alphabet. You’ll also learn some naive implementations that rely on the ord() and chr() functions.

Using the string Module to Make a Python List of the Alphabet

The simplest and, perhaps, most intuitive way to generate a list of all the characters in the alphabet is by using the string module. The string module is part of the standard Python library, meaning you don’t need to install anything. The easiest way to load a list of all the letters of the alphabet is to use the string.ascii_letters , string.ascii_lowercase , and string.ascii_uppercase instances.

As the names describe, these instances return the lower and upper cases alphabets, the lower case alphabet, and the upper case alphabet, respectively. The values are fixed and aren’t locale-dependent, meaning that they return the same values, regardless of the locale that you set.

Let’s take a look at how we can load the lower case alphabet in Python using the string module:

# Loading the lowercase alphabet to a list import string alphabet = list(string.ascii_lowercase) print(alphabet) # Returns: ['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']

Let’s break down how this works:

  1. We import the string module
  2. We then instantiate a new variable, alphabet , which uses the string.ascii_lowercase instance.
  3. This returns a single string containing all the letters of the alphabet
  4. We then pass this into the list() function, which converts each letter into a single string in the list

The table below shows the types of lists you can generate using the three methods:

Python List Comprehensions Syntax

We can see that we evaluate an expression for each item in an iterable. In order to do this, we can iterate over the range object between 97 and 122 to generate a list of the alphabet. Let’s give this a shot!

# Generate a list of the alphabet in Python with a list comprehensions alphabet = [chr(value) for value in range(97, 123)] print(alphabet) # Returns: ['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']

While our for loop wasn’t very complicated, converting it into a list comprehension makes it significantly easier to read! We can also convert our more dynamic version into a list comprehension, as show below:

# Generate a list of the alphabet in Python with a list comprehension alphabet = [chr(value) for value in range(ord('a'), ord('a') + 26)] print(alphabet) # Returns: ['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']

In the final section, you’ll learn how to use the map() function to generate a list of the alphabet in Python.

Using Map to Make a Python List of the Alphabet

In this section, you’ll make use of the map() function in order to generate the alphabet. The map function applies a function to each item in an iterable. Because of this, we can map the chr function to each item in the range covering the letters of the alphabet. The benefit of this approach is increased readability by being able to indicate simply what action is being taken on each item in an iterable.

Let’s see what this code looks like:

# Generate a list of the alphabet in Python with map and chr alphabet = list(map(chr, range(97, 123))) print(alphabet) # Returns: ['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']

Here, we use the map() function and pass in the chr function to be mapped to each item in the range() covering 97 through 123. Because the map() function returns a map object, we need to convert it to a list by using the list() function.

Conclusion

In this tutorial, you learned a number of ways to make a list of the alphabet in Python. You learned how to use instances from the string module, which covers lowercase and uppercase characters. You also learned how to make use of the chr() and ord() functions to convert between Unicode and integer values. You learned how to use these functions in conjunction with a for loop, a list comprehension, and the map() function.

Additional Resources

To learn more about related topics, check out the articles listed below:

Источник

Читайте также:  Intellij idea php project
Оцените статью