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
+16.5k Golang : Put UTF8 text on OpenCV video capture image frame
+4.9k Golang : List all packages and search for certain package
+9.5k Golang : How to transmit update file to client by HTTP request example
+11.4k Golang : zlib compress file example
+17.8k Golang : How to make function callback or pass value from function as parameter?
+36k Golang : Read a text file and replace certain words
+4.6k Golang : What is StructTag and how to get StructTag's value?
+23.9k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+28.9k Golang : Generate random string
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+11.4k Golang : Exit, terminating or aborting a program
+8.1k Golang : Generate Codabar