Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 'b' character do in front of a string literal in Python 3 [duplicate]

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' 
like image 950
Panagiotis Drakatos Avatar asked May 04 '16 01:05

Panagiotis Drakatos


People also ask

How do I remove leading b from a string in Python?

Decode() function is used to remove the prefix b of a string.

What does the b character do in front of a string literal in Python?

A prefix of 'b' or 'B' is ignored in Python 2. In Python 3, Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

How do I remove a character from a string in Python 3?

You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

How do I remove a specific character from a string in Python?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


1 Answers

This should do the trick:

pw_bytes.decode("utf-8") 
like image 190
krock Avatar answered Oct 15 '22 01:10

krock