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

The most time-saving and efficient way to list the ASCII characters is to use list comprehension. It is a method for creating a new list based on an existing list by performing a specific operation.

Since only 128 characters are in the ASCII character set, 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

As shown in the screenshot above, the output displays all ASCII characters in a list format on the console.

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 may be feasible to use a “for loop”.

However, I wouldn’t recommend using a 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 a “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. Traditionally, apply your standard logic using a 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. 

Additionally, they are limited to predefined sets, and they require us to import a “string” module in the first place, which, in some cases, is unnecessary for this task alone.

If you are interested in listing unicode point characters, check out this tutorial.

Post Views: 318
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.

How to Print All Unicode Characters with Python
How to Pad a String with Zeros in Python

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