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
+12.1k Golang : Image to ASCII art example
+16.1k Golang : Multi threading or run two processes or more example
+6.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+21.4k Golang : Test file read write permission example
+6.7k Golang : Grayscale Image
+4.3k Golang : The Tao of importing package
+7.1k Golang : Variadic function arguments sanity check example
+9k Golang : Random Rune generator
+12.2k Golang : Read XML elements data with xml.CharData example
+5.1k Golang : Debug with Godebug
+6k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+18.2k Golang : Delete item from slice based on index/key position