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
+6.3k Golang : Calculate how many weeks left to go in a given year
+6.5k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+33.1k Golang : Integer is between a range
+4.5k Swift : Convert string array to array example
+12.4k Golang : Get dimension(width and height) of image file
+5.3k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+5.7k Golang : Skip or discard items of non-interest when iterating example
+8.3k Golang : How to control fmt or log print format?
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+7.9k Golang : Build and compile multiple source files
+16.6k Golang : Aligning strings to right, left and center with fill example
+24.3k Golang : How to read integer value from standard input ?