GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Convert a byte string to a list of integers - w3resource

Python: Convert a byte string to a list of integers

Last update on May 17 2025 05:44:05 (UTC/GMT +8 hours)


String Bytes to Integers

Write a Python program to convert the bytes in a given string to a list of integers.

Sample Solution-1:

Python Code:

# Create a bytes object containing the bytes 'Abc'.
x = b'Abc'

# Print an empty line for clarity.
print()

# Convert the bytes of the said string to a list of integers and print the result.
print("Convert bytes of the said string to a list of integers:")
print(list(x))

# Print an empty line for clarity.
print()

Sample Output:

Convert bytes of the said string to a list of integers:
[65, 98, 99]

Sample Solution-2:

Python Code:

# Define a string named S.
S = "The quick brown fox jumps over the lazy dog."

# Print a message indicating the original string.
print("Original string:")

# Print the original string.
print(S)

# Create an empty list named nums.
nums = []

# Print a message to indicate the conversion of bytes to a list of integers.
print("\nConvert bytes of the said string to a list of integers:")

# Iterate through each character (byte) in the string S and append its ASCII value to the nums list.
for chr in S:
    nums.append(ord(chr))

# Print the list of integers.
print(nums)

Sample Output:

Original string:
The quick brown fox jumps over the lazy dog.

Convert bytes of the said string to a list of integers:
[84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103, 46]

For more Practice: Solve these Related Problems:

  • Write a Python program to convert a list of bytes into a string.
  • Write a Python program to find the sum of ASCII values of characters in a string.
  • Write a Python program to replace all characters in a string with their ASCII equivalents.
  • Write a Python program to convert a given integer list back to a byte string.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to get the identity of an object.
Next: Write a Python program to check if a string is numeric.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.