Open In App

Python bytes() method

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python.

Python
a = "geeks"

 # UTF-8 encoding is used
b = bytes(a, 'utf-8') 
print(b)

Output
b'geeks'

Syntax of bytes()

bytes([source[, encoding[, errors]]])

Parameters:

source (optional)

  • A string will be encoded into bytes using the specified encoding (default is 'utf-8').
  • An iterable will have each element converted into bytes but it must have integers between 0 and 255.
  • An integer creates a bytes object of that size with all elements initialized to 0.

encoding (optional)

This parameter is used when the source is a string. It specifies the encoding format to convert the string into bytes. The default encoding is 'utf-8' but you can use other encodings like 'ascii', 'utf-16', etc.

errors (optional)

Defines how to handle errors during the encoding process.

  • 'strict': Raises a UnicodeEncodeError if there is an error.
  • 'ignore': Ignores characters that can't be encoded.
  • 'replace': Replaces characters that can't be encoded with a placeholder (usually a question mark).

Return Type:  

  • return type of the bytes() method is a bytes object. A bytes object is an immutable sequence of integers in the range from 0 to 255.

Using Custom Encoding

If the string contains characters from other languages, we can specify a different encoding:

Python
a = "गीक्स"
b = bytes(a, 'utf-16')  

# Converts the string to bytes using UTF-16 encoding
print(b)

Output
b'\xff\xfe\x17\t@\t\x15\tM\t8\t'

Convert String to Bytes

Python String to bytes using the bytes() function, for this we take a variable with string and pass it into the bytes() function with UTF-8 parameters. When we pass a string to the bytes() method, we also need to tell Python which "language" (encoding) to use to convert the string.

Python
s = "Welcome to Geeksforgeeks"

b = bytes(s, 'utf-8')

print(b)

Output
b'Welcome to Geeksforgeeks'

Converting a List of Integers to Bytes

Each number in the list must be between 0 and 255 because each byte can only hold numbers in that range. When we pass a list of numbers to the bytes() method, Python will create a bytes object where each number in the list corresponds to one byte.

Python
a = [215, 66, 67]  

 # Converts the list of integers into bytes
b = bytes(a) 
print(b)

Output
b'\xd7BC'

Creating a Bytes Object with a Specific Size

By passing a single integer to bytes(), Python will create a bytes object of that length, filled with zero values (because a byte is initially 0).

Python
a = 5  

# Creates a bytes object of length 5, with all zero values
b = bytes(a)  
print(b)

Output
b'\x00\x00\x00\x00\x00'

Next Article

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy