By: BHUVANESHWARI
In python , the ‘b’ character before a string is used to specify the string as a “byte string”. By adding the ‘b’ character before a string literal, it becomes a bytes literal. The string content should be a series of bytes and not character. Bytes literals are utilized for representing binary data like encoded text, pictures, audio, or any other type of data that may not directly correspond to character.
Example:
d=b'CodeSpeedy' print(d)
Output :
b'CodeSpeedy'
Byte Literal: A bytes literal is prefixed with ‘b’ or ‘B’ and is used to handle binary data. For Example ‘b’hello’ ‘ represents the byte sequence corresponding to the ASCII values of the characters in the string “hello”. We will use Bytes Literal Network communication where data is often transmitted as bytes. and Interfacing with low-level system APIs. Cryptographic operations where data needs to be handled in a precise byte-wise manner.
Creating a Byte Literal:
byte_string = b'hello' print(byte_string)
Output:
b'hello'
string='CodeSpeedy' byte_string=string.encode('utf-8') print(byte_string)
Output:
b'hello'
Example: Here is the code implementation of showing the difference between string and string in python.
b_string='CodeSpeedy' print("Original String:", b_string) print("Type of String:", type(b_string)) Byte_strin=b'CodeSpeey' print("Original Byte String:", byte_string) print(Type of Byte String:", type(byte_string))
Output:
Original String: CodeSpeedy Type of String: <class str'> Original Byte String: b'CodeSpeedy' Type of Byte String: <class 'bytes'>