Python : Convert(cast) bytes to string example
Problem :
You want to convert or type cast bytes variable to type string variables in Python. How to do that?
Solution :
For Python 3 :
To convert bytes to string, just type cast with the str
pre-fix. For example :
var = b"this is a string"
type(var)
<class 'bytes'>
var2 = str(var)
type(var2)
<class 'str'>
You can convert the bytes variable straightaway by adding the 'str' pre-fix.
var = str(b"this is a string")
type(var)
<class 'str'>
Happy Coding!
See also : Python : Convert(cast) string to bytes example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+7.3k Golang : Convert source code to assembly language
+8.9k Golang : Capture text return from exec function example
+20.9k Golang : For loop continue,break and range
+16.3k Golang : How to implement two-factor authentication?
+5.7k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+6.2k Golang : Test input string for unicode example
+32.8k Golang : How to check if a date is within certain range?
+7.4k Golang : Dealing with struct's private part
+6.8k Golang : Calculate BMI and risk category
+14.2k Golang : How to shuffle elements in array or slice?
+13.8k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error