Как вывести алфавит в python
Перейти к содержимому

Как вывести алфавит в python

  • автор:

List the Alphabet in Python

List the Alphabet in Python

This tutorial shows you how to list the alphabet by the range in Python.

In this tutorial, we want to store the English alphabet’s 26 lowercase characters in a Python list. The quickest way to solve this problem is by making use of the ASCII values of each character and using pre-existing functions in Python.

Use Utils From Module string to List the Alphabet in Python

The Python module string is readily available and contains pre-defined constant values that we can use for this problem. The constant string.ascii_lowercase contains all 26 lowercase characters in string format.

If you perform print(string.ascii_lowercase) , it will result in the following output:

Therefore, we can use this constant and convert it into a list of characters to produce a list of the alphabet.

If you prefer the alphabet list to be in uppercase, then you should use string.ascii_uppercase and reuse the code above and will produce the same output, but in uppercase format.

Use range() to List the Alphabet in Python

range() is a function that outputs a series of numbers. You can specify when the function starts and stops with the first and second arguments.

range() and map()

map() is a function that accepts two arguments: the second argument of the function is an iterable or a collection; the first argument is a function to iterate over and handle the second argument.

We’re going to use these two methods to generate a list of the alphabet using the lowercase letters’ ASCII values and map them with the function chr() , which converts integers into their ASCII counterpart.

List of Alphabets Python

report this ad

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).

Please enable JavaScript

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

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.

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.

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

Print list from a to 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.

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.

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 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.

Table of Contents

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:

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!

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:

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:

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.

Alphabet range in Python

How do I create a list of alphabet characters, without doing it manually like this?

Mateen Ulhaq's user avatar

9 Answers 9

Alternatively, using range :

Other helpful string module features:

Mateen Ulhaq's user avatar

jamylak's user avatar

Bg1850's user avatar

In Python 2.7 and 3 you can use this:

As @Zaz says: string.lowercase is deprecated and no longer works in Python 3 but string.ascii_lowercase works in both

Trinh Nguyen's user avatar

Here is a simple letter-range implementation:

Code

Demo

pylang's user avatar

If you are looking to an equivalent of letters[1:10] from R, you can use:

Tiago Martins Peres's user avatar

Qaswed's user avatar

This is the easiest way I can figure out:

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.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *