Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Convert a Byte String to a List
A byte string is similar to a string (Unicode), except instead of characters, it contains a series of bytes. Applications that handle pure ASCII text rather than Unicode text can use byte strings.
Converting a byte string to a list in Python provides compatibility, modifiability, and easier access to individual bytes. It allows seamless integration with other data structures, facilitates modification of elements, and enables efficient analysis and manipulation of specific parts of the byte string.
Demonstration
Assume we have taken an input byte string. We will now convert the given byte string to the list of ASCII values as shown below ?
Input
inputByteString = b'Tutorialspoint'
Output
List of ASCII values of an input byte string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Methods Used
The following are the various methods to accomplish this task ?
Using the list() function
Using the for loop and ord() function
Using list comprehension
Method 1: Using the list() Function
The simplest approach is to use the built-in list() function which directly converts a byte string to a list of ASCII values ?
Syntax
list([iterable])
Example
The following program converts the input byte string into a list and returns the list of ASCII values ?
# input byte string
inputByteStr = b'Tutorialspoint'
# converting the input byte string into a list
# which contains the ASCII values as a list
print("List of ASCII values of an input byte string:")
print(list(inputByteStr))
List of ASCII values of an input byte string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Method 2: Using For Loop and ord() Function
For regular strings (not byte strings), you can use a for loop with the ord() function to get ASCII values ?
ord() Function
Returns the Unicode code of a given character as a number.
Syntax
ord(char)
Example
The following program converts a regular string into a list of ASCII values using the for loop and ord() function ?
# input string (not byte string)
inputStr = 'Tutorialspoint'
# empty list for storing the ASCII values
resultantList = []
# traversing through each character of the input string
for c in inputStr:
# getting the ASCII value of each character using the ord() function
# and appending it to the resultant list
resultantList.append(ord(c))
# Printing the ASCII values of input string
print("List of ASCII values of an input string:", resultantList)
List of ASCII values of an input string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Method 3: Using List Comprehension
List comprehension provides a concise way to convert a byte string to a list ?
Example
The following program uses list comprehension to convert a byte string into a list of ASCII values ?
# input byte string
inputByteStr = b'Tutorialspoint'
# converting byte string to list using list comprehension
ascii_values = [byte for byte in inputByteStr]
print("List of ASCII values using list comprehension:")
print(ascii_values)
# Alternative: convert regular string using list comprehension
inputStr = 'Tutorialspoint'
ascii_values_str = [ord(char) for char in inputStr]
print("List of ASCII values from string:")
print(ascii_values_str)
List of ASCII values using list comprehension: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116] List of ASCII values from string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116]
Comparison
| Method | Input Type | Best For | Performance |
|---|---|---|---|
list() |
Byte string | Simple conversion | Fast |
for loop + ord() |
Regular string | Step-by-step control | Moderate |
| List comprehension | Both | Concise syntax | Fast |
Conclusion
Use list() for direct byte string conversion, or list comprehension for more complex transformations. The ord() function is useful when working with regular strings to get ASCII values.
