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.6k Golang : Humanize and Titleize functions
+16.6k Golang : Gzip file example
+6.7k Swift : substringWithRange() function example
+17.7k Golang : Iterate linked list example
+18.3k Golang : Aligning strings to right, left and center with fill example
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+15.1k Golang : How to get Unix file descriptor for console and file
+15.2k Golang : Find location by IP address and display with Google Map
+10.5k Golang : Get local time and equivalent time in different time zone
+13.7k Golang : unknown escape sequence error
+4.8k HTTP common errors and their meaning explained
+5.5k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday