Convert Unicode to Bytes in Python
Last Updated :
05 Feb, 2024
Unicode, often known as the Universal Character Set, is a standard for text encoding. The primary objective of Unicode is to create a universal character set that can represent text in any language or writing system. Text characters from various writing systems are given distinctive representations by it.
Convert Unicode to Byte in Python
Below, are the ways to convert a Unicode String to a Byte String In Python.
- Using
encode()
with UTF-8 - Using
encode()
with a Different Encoding - Using
bytes()
Constructor - Using
str.encode()
Method
Convert A Unicode to Byte Using encode()
with UTF-8
In this example, the Unicode string "Hello, GeeksforGeeks" is encoded into bytes using the UTF-8 encoding with the `encode()` method. The resulting `bytes_representation` is a sequence of bytes representing the UTF-8 encoded version of the original string.
Python3
unicode_string = "Hello, GeeksforGeeks"
bytes_representation = unicode_string.encode('utf-8')
print(bytes_representation)
Outputb'Hello, GeeksforGeeks'
Unicode To Byte Using encode()
with a Different Encoding
In this example, the Unicode string is encoded into a byte string using the UTF-16 encoding with the `encode()` method. The resulting `byte_string_utf16` contains the UTF-16 encoded representation of the original string, which is then printed to the console.
Python3
unicode_string = "Hello, Noida"
byte_string_utf16 = unicode_string.encode('utf-16')
# Displaying the byte string
print(byte_string_utf16)
Outputb'\xff\xfeH\x00e\x00l\x00l\x00o\x00,\x00 \x00N\x00o\x00i\x00d\x00a\x00'
Convert A Unicode String To Byte String Using bytes()
Constructor
In this example, the Unicode string is converted to a byte string using the bytes() constructor with UTF-8 encoding. The resulting `byte_string_bytes` represents the UTF-8 encoded byte sequence of the mixed-language Unicode string.
Python3
unicode_string = "Hello, 你好"
byte_string_bytes = bytes(unicode_string, 'utf-8')
# Displaying the byte string
print(byte_string_bytes)
Outputb'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
Python Unicode String to Byte String Using str.encode()
Method
In this example, the Unicode string is transformed into a byte string using the str.encode() method with UTF-8 encoding. The resulting `byte_string_str_encode` represents the UTF-8 encoded byte sequence of the mixed-language Unicode string.
Python3
unicode_string = "Hello, Shivang"
byte_string_str_encode = str.encode(unicode_string, 'utf-8')
# Displaying the byte string
print(byte_string_str_encode)
Similar Reads
Convert Bytes To Bits in Python Converting bytes to bits in Python involves representing each byte in its binary form, where each byte is composed of 8 bits. For example , a byte like 0xAB (which is 171 in decimal) would be represented as '10101011' in binary. Letâs explore a few techniques to convert bytes to bits in Python.Using
2 min read
Convert Unicode String to a Byte String in Python Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular
2 min read
How to Convert Int to Bytes in Python? The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
How to Convert Bytes to Int in Python? Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
How to Convert Bytes to String in Python ? We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'.This article covers different ways to convert bytes into strings in Pyt
2 min read
Convert Hex String to Bytes in Python Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD. Letâs explore a few techniques to convert a hex string to bytes.Using bytes.fromhex() bytes.fro
2 min read