How to Convert a String to ASCII in Python?

In this tutorial, I will explain how to convert a string to ASCII in Python. I encountered this issue while developing a data analytics system that involved processing textual data from legacy devices across multiple locations in the United States. These devices transmitted information in ASCII format, and converting user-input strings into ASCII values was necessary to ensure compatibility with the system. Let us explore how to effectively convert strings to ASCII in Python.

ASCII in Python

ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents each character as a number between 0 and 127. It covers English letters, digits, punctuation, and some control characters. Any character outside of this range is considered a non-ASCII character.

Read Find the First Number in a String in Python

Use ord() Function

In Python, we can use the built-in ord() function to convert a single character to its corresponding ASCII value.

For example, let’s say we have a string containing a person’s name, like “John”. To convert each character to its ASCII value, we can use the following code:

name = "John"
ascii_values = [ord(char) for char in name]
print(ascii_values) 

Output:

[74, 111, 104, 110]

I have executed the above example code and added the screenshot below.

Convert a String to ASCII in Python

In this example, we use a list comprehension to iterate over each character in the string and apply the ord() function to obtain its ASCII value. The resulting ascii_values list contains the ASCII values for each character in the string.

Read How to Compare Strings in Python?

Use a Lambda Function

Another approach to convert a string to ASCII is by using a lambda function in combination with the map() function. Let’s consider an example where we have a list of cities in the USA:

cities = ["New York", "Los Angeles", "Chicago", "Houston"]
ascii_cities = [list(map(lambda char: ord(char), city)) for city in cities]
print(ascii_cities)

Output:

[[78, 101, 119, 32, 89, 111, 114, 107],
 [76, 111, 115, 32, 65, 110, 103, 101, 108, 101, 115],
 [67, 104, 105, 99, 97, 103, 111],
 [72, 111, 117, 115, 116, 111, 110]]

I have executed the above example code and added the screenshot below.

How to Convert a String to ASCII in Python

In this example, we define a lambda function that takes each character and converts it to its ASCII value using the ord() function. We then apply this lambda function to each character in each city name using the map() function. Finally, we convert the map object to a list to obtain the ASCII values for each city.

Read How to Create a String of N Characters in Python?

Handle Sentences and Paragraphs

When dealing with longer text, such as sentences or paragraphs, we can extend the previous approaches to convert the entire text to ASCII. Let’s consider an example where we have a sentence about a popular tourist attraction in the USA:

sentence = "The Statue of Liberty is a colossal neoclassical sculpture on Liberty Island in New York Harbor."
ascii_sentence = [ord(char) for char in sentence]
print(ascii_sentence)

Output:

[84, 104, 101, 32, 83, 116, 97, 116, 117, 101, 32, 111, 102, 32, 76, 105, 98, 101, 114, 116, 121, 32, 105, 115, 32, 97, 32, 99, 111, 108, 111, 115, 115, 97, 108, 32, 110, 101, 111, 99, 108, 97, 115, 115, 105, 99, 97, 108, 32, 115, 99, 117, 108, 112, 116, 117, 114, 101, 32, 111, 110, 32, 76, 105, 98, 101, 114, 116, 121, 32, 73, 115, 108, 97, 110, 100, 32, 105, 110, 32, 78, 101, 119, 32, 89, 111, 114, 107, 32, 72, 97, 114, 98, 111, 114, 46]

I have executed the above example code and added the screenshot below.

Convert a String to ASCII in Python handle sentences

Here, we directly apply the list comprehension to convert each character in the sentence to its ASCII value. The resulting ascii_sentence list contains the ASCII values for each character in the sentence, including spaces and punctuation marks.

Read How to Split a String into Equal Parts in Python?

Conclusion

In this tutorial, I have explained how to convert a string to ASCII in Python. I discussed ASCII and methods to convert strings to ASCII like using the ord() function, lambda function, and we saw how to handle sentences and paragraphs.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.