![]() |
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 string. In this article, we will explore five different methods to achieve this in Python. Convert A Unicode String to a Byte String In PythonBelow, are the ways to convert a Unicode String to a Byte String In Python.
Convert A Unicode to a Byte String Using
|
unicode_string = "Hello, 你好" bytes_representation = unicode_string.encode( 'utf-8' ) print (bytes_representation) |
b'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
encode()
with a Different EncodingIn this example, the Unicode string is encoded into a byte string using UTF-16 encoding, resulting in a sequence of bytes that represents the mixed-language string. The byte string is then printed to demonstrate the UTF-16 encoded representation of the Unicode characters.
unicode_string = "Hello, 你好" byte_string_utf16 = unicode_string.encode( 'utf-16' ) # Displaying the byte string print (byte_string_utf16) |
b'\xff\xfeH\x00e\x00l\x00l\x00o\x00,\x00 \x00`O}Y'
bytes()
ConstructorIn 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.
unicode_string = "Hello, 你好" byte_string_bytes = bytes(unicode_string, 'utf-8' ) # Displaying the byte string print (byte_string_bytes) |
b'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
str.encode()
MethodIn 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.
unicode_string = "Hello, 你好" byte_string_str_encode = str .encode(unicode_string, 'utf-8' ) # Displaying the byte string print (byte_string_str_encode) |
b'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |