- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
bytes in Python
bytes in Python are of the binary sequence type. Here is the list of three binary sequence types available in Python:
- bytes
- bytearray
- memoryview
bytes are the fundamental inbuilt types used in binary data manipulation, such as bytearray. Unlike bytearray, bytes objects are immutable sequences of single bytes, which are basically arrays of octets, used for storing the data but not the text.
Note: bytes objects are similar to string objects. The difference is: values of bytes literals come with a "b" prefix. For example: b'codes', b'fresherearth dot com', and so on.
Important: Only ASCII characters are allowed in bytes literals.
Creating bytes literals in Python
A "bytes" literal can be created in one of the following ways:
- using single quotes
- using double quotes
- using three single quotes
- using three double quotes
The b prefix is required in all the cases.
Python bytes example
Here is an example of bytes in Python. This program generates bytes objects in all of the ways described above. This program creates a bytes object and prints the value along with the type using the type() method.
x = b'allows embedded "double" quotes' print(x) print(type(x)) x = b"allows embedded 'single' quotes" print(x) print(type(x)) x = b'''three single quotes''' print(x) print(type(x)) x = b"""three double quotes""" print(x) print(type(x))
The snapshot given below shows the sample run of the above Python program, demonstrating the creation of bytes in Python:
ASCII Code Representing the Character of a bytes Object in Python
This program is created to receive a string input from the user at run-time and print the ASCII code corresponding to all the characters available in the given string:
print("Enter the String: ", end="") s = input() s = bytes(s, "utf-8") print(list(s))
The snapshot given below shows the sample run with user input, say "fresherearth" as a string to list and print the ASCII values of all characters:
Python bytes Object Slicing
Just like a list, a bytes object can also be sliced to get some part of the bytes object value. Here is an example of bytes slicing.
x = b"python programming" print(x) print(x[0:]) print(x[-18:]) print(x[0:6]) print(x[7:14])
The 0th index refers to the index of the very first element. Adding the minus (-) sign before the index refers to the native or backward indexing. That is, -1 refers to the index of the very first element from last, whereas -18 refers to the 18th element's index from last. The output produced by the above program should be:
b'python programming' b'python programming' b'python programming' b'python' b'program'
Note: For more information on slicing, see the separate tutorial on list .You'll find all the information there.
To convert a bytes object to a string object, refer to the Python bytes to string program.
« Previous Tutorial Next Tutorial »