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'...'

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

import hashlib

text = "my secret data"
pw_bytes = text.encode('utf-8')
print('print',pw_bytes)
m = hashlib.md5()
m.update(pw_bytes)

OUTPUT:

 print b'my secret data'

asked May 4, 2016 at 1:27

Hướng dẫn python remove b prefix

1

This should do the trick:

pw_bytes.decode("utf-8")

answered May 4, 2016 at 1:33

krockkrock

28.2k12 gold badges76 silver badges84 bronze badges

7

Here u Go

f = open('test.txt','rb+')
ch=f.read(1)
ch=str(ch,'utf-8')
print(ch)

answered Feb 17, 2017 at 16:57

Hướng dẫn python remove b prefix

3

Decoding is redundant

You only had this "error" in the first place, because of a misunderstanding of what's happening.

You get the b because you encoded to utf-8 and now it's a bytes object.

 >> type("text".encode("utf-8"))
 >> 

Fixes:

  1. You can just print the string first
  2. Redundantly decode it after encoding

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

>>> t=subprocess.check_output('ls', shell=True)
>>> t
b'compress.py\n'
>>> b=t.decode('utf-8')
>>> b
'compress.py\n'

Approach 2: Using str() function

>>> t=subprocess.check_output('ls', shell=True)
>>> t
b'compress.py\n'
>>> a=str(t,'utf-8')
>>> a
'compress.py\n'

If you are familiar with Python, you may be well aware of the different Python data types including strings and bytes.
While programming, you may have come across strings with “b” as a prefix. What exactly is this b in front of the string? What does it indicate and why is it used? Further, how do you remove the same?

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 Python

Using the decode() method: Converting byte string back to a character string

string1 = b'CodeSpeedy'
print(string1)
print(type(string1))
string1_aft_decode = string1.decode()
print(string1_aft_decode)
print(type(string1_aft_decode))
b'CodeSpeedy'

CodeSpeedy

Using the codecs module: Converting byte string back to a character string

import 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'

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