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
+15.8k Golang : Get sub string example
+18.4k Golang : Generate thumbnails from images
+12.8k Swift : Convert (cast) Int to String ?
+6.4k Golang : How to determine if request or crawl is from Google robots
+8.1k Golang : Number guessing game with user input verification example
+12.2k Golang : Search and extract certain XML data example
+6k Golang : Extract XML attribute data with attr field tag example
+17.4k Golang : [json: cannot unmarshal object into Go value of type]
+13.2k Golang : Count number of runes in string
+36.4k Golang : Display float in 2 decimal points and rounding up or down
+13.2k Android Studio : Password input and reveal password example
+7.8k Golang : Grayscale Image