Prefix b Before String in Python

Prefix b Before String in Python

Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python.

The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and quite a few changes were done to the existing types. One such change was the distinction between bytes and strings in Python 3.

In Python 2, bytes and strings were considered the same, and both were used interchangeably. They were considered as a collection of octets in this version which means that they were ASCII encoded. To create Unicode string literals one had to use the u prefix before the string.

However, Python 3 introduced a major change as strings were now considered a collection of Unicode characters. This provided users with a wide range of characters to choose from and represent strings as texts.

Bytes are treated as a separate entity in Python 3 and we have to use the prefix b before string in Python to create them. The b prefix tells the interpreter to create a bytes object.

For example,

Output:

b’bytes string’

In the above example, we can notice the prefix b before string in Python which indicates that it is a byte string.

We can also use the bytes() and bytearray() functions to create a bytes instance of the required sequence. Here we need to specify the source for the object. If the source is a string, we need to specify the encoding of the same as well.

The difference between the above-mentioned functions is that the object returned by the bytes() function cannot be modified whereas the object returned by the latter can be altered.

Conclusion

In this tutorial, we discussed the prefix b before string in Python. First, we highlighted the difference in Python 2 and 3 regarding the treatment of bytes. We discussed the distinction between bytes and strings in Python 3 and how the prefix b before string in Python can create a bytes instance. We also highlighted the use of bytes() and bytearray() functions.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 December

    Encode String to UTF-8 in Python

    Table of ContentsEncode String to UTF-8 in PythonUsing the encode() functionUsing the codecs.encode() functionConclusion The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This […]

  • 23 December

    How To Do Line Continuation in Python

    Table of ContentsUsing Backslash (\) OperatorLine Continuation in StringLine Continuation in Number ExpressionUsing Parentheses ()Line Continuation in StringLine Continuation in Number Expression Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63dc5a5f72e35833125089/] [crayon-63dc5a5f72e3a900709787/] First, we created a […]

  • 09 December

    Count Occurrences of Character in String in Python

    Table of ContentsUsing count() MethodUsing Naive for loopUsing List ComprehensionUsing re.findall() MethodUsing for loop with re.finditer() MethodUse the lambda Function with map()Use collections.Counter() Method Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. [crayon-63dc5a5f72fba841499525/] We […]

  • 08 December

    Find Character in String in Python

    Table of ContentsUsing find() MethodUsing index() MethodUsing for Loop with re.finditer() MethodUsing list comprehension Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned […]

  • 05 December

    Remove Substring from String in Python

    Table of ContentsUse replace() MethodUse the split() Method with for loopUse split() with join() MethodUse re.sub() Method Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") [crayon-63dc5a5f7321a595031593/] The code […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.