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
+13k Golang : http.Get example
+6.2k PHP : How to check if an array is empty ?
+14.5k Golang : Recombine chunked files example
+12k Golang : Verify Linux user password again before executing a program example
+8.1k Swift : Convert (cast) String to Float
+23.1k Golang : Gorilla mux routing example
+19.9k Golang : Get current URL example
+8.5k Golang : Auto-generate reply email with text/template package
+7.5k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+9.2k Golang : Go as a script or running go with shebang/hashbang style
+6.6k Golang : How to search a list of records or data structures
+14.8k Golang : Convert(cast) int to float example