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
+59.4k Golang : Convert HTTP Response body to string
+5.6k Golang : Check to see if *File is a file or directory
+7.4k Golang : Convert octal value to string to deal with leading zero problem
+8.6k Golang : Find and draw contours with OpenCV example
+5.2k Golang : Array mapping with Interface
+4.3k Golang : Extract unicode string from another unicode string example
+11.3k Golang : Check if an integer is negative or positive
+21.3k Golang : Convert uint value to string type
+7.7k Golang : Convert(cast) string to int64
+24.8k Golang : How to convert(cast) string to IP address?
+5.4k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image