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
+5.4k Golang : Break string into a slice of characters example
+14.7k Golang : Read a file line by line
+12.1k Swift : Convert (cast) Int to String ?
+3.1k Detect if Google Analytics and Developer Media are loaded properly or not
+6.4k Golang : Not able to grep log.Println() output
+21.4k Golang : Test file read write permission example
+17.8k Golang : Fix cannot download, $GOPATH not set error
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+7.7k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+7.9k Golang : How to extract video or image files from html source code
+14.1k Golang : How to login and logout with JWT example