In modern times, Unicode points are the diamond standard. However, there are some legacy systems that deal with ASCII characters. ASCII system is the fundamental block upon which various encoding character systems are built.
If you are working with older file system that uses ASCII system then you should have the knowledge of ASCII characters and how to manipulate it.
Furthermore, if you are doing data analysis, eventually, you need to filter out non-ASCII characters to make them ASCII-compliant.
Here’s how you can get a List of All the ASCII characters in three different ways:
Method 1: List Comprehension
The most time-saving and efficient way to list the ASCII characters is to use list comprehension. It is a way to create a new list based on the existing list by performing a specific operation.
Since only 128 characters are there in ASCII, it is efficient to create a list in memory, which is why it is the fastest approach. However, if the list is extensive, this approach might not be a good fit for you.
Here’s the exact code implementation showing that:
ascii_characters = [chr(i) for i in range(128)] print(ascii_characters)
Output
You can observe from the output shown in the above screenshot that all the ASCII characters are printed on the console in the form of a list. Each list element is an ASCII character. Performance-wise, this approach is the best.
One drawback of this approach is that you need to get familiar with list comprehension, which is more complex than using a simple for loop.
Method 2: Traditional for loop
In our case, when we are only dealing with 128 characters, it might be feasible to use “for loop”, but I wouldn’t recommend using for loop for every programming situation. It will cost you a performance.
ascii_characters = [] for ch in range(128): ascii_characters.append(chr(ch)) print(ascii_characters)
Output
And we get the expected output, but, this time using “for loop”. A key advantage of using a for loop is that you don’t need to take an extra step to learn any new programming concept. Just use your normal logic in traditional way using for loop and everything is set.
Method 3: String Module
Moving on from built-in methods, let’s discuss using a “string” module. The string module provides various props and methods to get the “pre-defined character sets,” which is extremely helpful when reading.
For example, you can use the “string.ascii_letters” that only returns the letters.
import string ascii_chars = list(string.ascii_letters + string.digits + string.punctuation + string.whitespace) print(ascii_chars)
Output
You can see from the output screenshot that we got what we wished for. However, it may include the characters you don’t need. Plus, they are limited to predefined sets, and it requires us to import a “string” module in the first place, which in some cases is unnecessary just for this task.
Conclusion
In this article, we have discussed three main methods to list all the ascii characters and print them in the console. The selection of a method should align with the specific needs of your project, encompassing factors like readability, maintainability, performance needs, and memory constraints.
If you are interested in listing unicode point characters, check out this tutorial.