Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Get a List of All the ASCII characters in Python

  • 27 Aug, 2024
  • Com 0

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.

Method 1 - List Comprehension

Here’s the exact code implementation showing that:

ascii_characters = [chr(i) for i in range(128)]

print(ascii_characters)

Output

Output of listing of all the ASCII characters in Python

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

Output of listing of all the ASCII characters using for loop

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

Using String Module

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.

Post Views: 114
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Printing All Unicode Characters with Python
Emptying an Array with JavaScript [6 Different Ways]

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend