Storage of Strings in ASCII
Published by
sanya sanya
It is frequently required to keep strings in a specified encoding format when working with them in Python. Among the most used encoding formats for text data storage in computers is ASCII (American Standard Code for Information Interchange). We will go through ASCII encoding, how to encode and decode texts in Python using ASCII, and some examples to help explain the idea in this blog.
What does ASCII coding mean?
Each letter of the English alphabet, the numerals, and a few special characters are represented by the ASCII encoding system, which uses a particular 7-bit binary code. This means that a number between 0 and 127 can be used to represent each character. The remaining 95 codes correspond to printable characters, while the first 32 codes are control codes (such as carriage return and line feed).
A popular encoding format for text data storage is ASCII, especially in embedded and older systems with constrained memory and processing capability. As it cannot represent characters from languages with different character sets, such as Chinese or Japanese, ASCII has some drawbacks.
ASCII Code for a String
Each ASCII character is represented by a 7-bit binary number, which consists of a series of seven zeros and ones. As a result, ASCII can represent 128 different character combinations. Control characters, such as the null character, end-of-line character, and backspace character, make up the first 32 characters (0–31). The remaining 96 characters (between 32 and 127) can be printed and include letters, numbers, punctuation, and symbols.
Each character in a string is replaced with its associated ASCII code to convert it to ASCII code. For instance, the phrase "Hello, world!" can be written as follows in ASCII code:
H = 72
E = 101
L = 108
L = 108
O = 111
, = 44
= 32
W = 119
O = 111
R = 114
L = 108
D = 100
! = 33
The string "Hello, world!" would have the following ASCII code:
72 101 108 108 111 44 32 119 111 114 108 100 33
These values would be kept in computer memory as an array of 8-bit bytes, where each byte corresponded to one ASCII code. For instance, the ASCII code for the letter 'H' is 72, which is equivalent to the binary value 01001000.
Even though ASCII code only permits 128 characters, it is nevertheless frequently used today despite its limitations in representing characters from languages other than English. Since then, a more thorough character encoding scheme called Unicode has been created to support a broader variety of languages and scripts. However, ASCII code continues to be a crucial component of computing and is a fundamental idea for anyone learning programming.
Here is a brief Python code example that shows how to translate a string to its ASCII equivalent:
https://images.codingblocks.com/data-science/ascii_1.png
In this code, we begin by creating a variable called string that holds the string that we want to encode as ASCII characters. Additionally, we declare the ASCII codes for each character in the string in an empty string called ascii_code.
Then, using a for loop to iterate through each character in the string, we convert each character to its matching ASCII code using the built-in Python function ord(). The Unicode code point of a character is returned as an integer by the ord() method.
Using the str() function, we turn the numeric value into a string and concatenate it with a space character to separate each ASCII code in the output string.
The ASCII codes for each character in the original string are then printed out in the ascii_code string. This code would provide the following results for the string "Hello, world!"
72 101 108 108 111 44 32 119 111 114 108 100 33
Keep in mind that the ord() function only supports ASCII character set characters. You will encounter a TypeError error if you attempt to convert a non-ASCII character.
ASCII String Encoding and Decoding
Python's encode() function, which turns a Unicode string into a bytes object encoded in the specified encoding format, can be used to encode a string in ASCII. The following is the syntax for encoding a string in ASCII:
https://images.codingblocks.com/data-science/ascii_2.png
Let's ASCII-encode the phrase "Hello, World!" as an illustration:
https://images.codingblocks.com/data-science/ascii_3.png
The b before the encoded string in this instance denotes that it is a bytes object.
The decode() method, which changes a bytes object into a Unicode string, can be used to decode an ASCII string. The following syntax is used to decode an ASCII bytes object:
https://images.codingblocks.com/data-science/ascii_4.png
Let's decode the bytes object "Hello, World!" back to a Unicode string as an illustration:
https://images.codingblocks.com/data-science/ascii_5.png
Examples
Let's examine some ASCII string encoding and decoding samples.
Example 1: ASCII encoding a string
https://images.codingblocks.com/data-science/ascii_6.png
Example 2: Decoding an ASCII Bytes Object
https://images.codingblocks.com/data-science/ascii_7.png
Example 3: Managing Non-ASCII Characters
Only the letters of the English alphabet and a few special characters can be represented using ASCII encoding. A UnicodeEncodeError will appear if we attempt to encode a string that contains non-ASCII characters.
https://images.codingblocks.com/data-science/ascii_8.png
We can handle non-ASCII characters in two ways: either by encoding the string in a different format that can accept a larger variety of characters, such UTF-8 or UTF-16, or by removing the non-ASCII characters first.
https://images.codingblocks.com/data-science/ascii_9.png
Conclusion
This blog post covered the definition of ASCII encoding, how to use it to encode and decode texts in Python, and several examples to help clarify the idea. For storing text data in computers, particularly in legacy systems and embedded systems, ASCII encoding is a frequently used encoding standard. It has several restrictions, though, as it cannot display characters from languages with distinct character sets. Based on the needs of the project, it is crucial to select the right encoding format.
Library
WEB DEVELOPMENT
FAANG QUESTIONS
On this page
What does ASCII coding mean?
ASCII Code for a String
ASCII String Encoding and Decoding
Examples
Example 1: ASCII encoding a string
Example 2: Decoding an ASCII Bytes Object
Example 3: Managing Non-ASCII Characters
Conclusion