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
+13.5k Golang : Login and logout a user after password verification and redirect example
+7.3k Android Studio : Create custom icons for your application example
+4.6k Golang : Get Alexa ranking data example
+33k Golang : Comparing date or timestamp
+4k Golang : Get missing location after unmarshal binary and gob decode time.
+15.6k Golang : Create and resolve(read) symbolic links
+3.1k Unix/Linux : How to find out the hard disk size?
+39.2k Golang : How to convert JSON string to map and slice
+22.5k Golang : Saving(serializing) and reading file with GOB
+7.3k Golang : Bcrypting password
+24.7k Golang : Get first few and last few characters from string