Hướng dẫn python remove b prefix
I am new in python programming and i am a bit confused. I try to get the bytes from a string to hash and encrypt but i got
b character in front of string just like the below example. Is any way avoid this?.Can anyone give a solution? Sorry for this silly question
OUTPUT:
asked May 4, 2016 at 1:27
1 This should do the trick:
answered May 4, 2016 at 1:33
krockkrock 28.2k12 gold badges76 silver badges84 bronze badges 7 Here u Go
answered Feb 17, 2017 at 16:57
3 Decoding is redundant You only had this "error" in the first place, because of a misunderstanding of what's happening. You get the
Fixes:
Robert Harvey 175k46 gold badges329 silver badges491 bronze badges answered May 4, 2016 at 1:31
PythonistaPythonista 11.2k2 gold badges30 silver badges49 bronze badges 5 You can try one of the following approaches to remove b' from a byte string. As a result, you will get a string. Approach 1: Using decode() function
Approach 2: Using str() function
If you are familiar with Python, you may be well aware of the different Python data types including strings and bytes. Well, let us find answers to all these questions by reading further into this tutorial. What is ‘b’ in front of string?The ‘b’ notations before a string are used to declare the particular string as a byte string. A byte string is nothing but an array of bytes consisting of integers between 0 and 255. On the other hand, a character string is simply a sequence of characters. In Python version 2 and below, the b prefix is usually ignored. However, in version 3, the same instantiates/specifies a byte object instead of a regular string object. You can understand the same by looking into the below demonstration: string1 = 'CodeSpeedy' print(type(string1)) string2 = b'CodeSpeedyl' print(type(string2)) Thus, though both the strings appear to be the same, their data types are different. Here is another demonstration in which we feed a character string as input and then convert it to a byte string. string = 'This is a string' print(string.encode()) b'This is a string' Thus, the string converted to byte string is preceded by ‘b’ as shown. How to remove b’ from the prefix of a string in PythonUsing the decode() method: Converting byte string back to a character stringstring1 = b'CodeSpeedy' print(string1) print(type(string1)) string1_aft_decode = string1.decode() print(string1_aft_decode) print(type(string1_aft_decode)) b'CodeSpeedy' Using the codecs module: Converting byte string back to a character stringimport codecs string1 = b'CodeSpeedy' print(string1) print(type(string1)) string1_aft_decode = codecs.decode(string1) print(string1_aft_decode) print(type(string1_aft_decode)) b'CodeSpeedy' Thus on being converted to a normal string, the ‘b’ in the prefix is automatically gone. Also read, Encoding and Decoding Base64 Strings in Python A related topic you can check out How to use Python Raw Strings |