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
+5.7k Golang : Sort and reverse sort a slice of integers
+2.6k Unix/Linux/MacOSx : Get local IP address
+13.3k Golang : Set or Add HTTP Request Headers
+6.6k Swift : Convert (cast) Float to String
+2.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+10.9k Golang : Convert long hexadecimal with strconv.ParseUint example
+3.7k Golang : Qt splash screen with delay example
+4.8k Android Studio : Simple input textbox and intercept key example
+4k Golang : Regular Expression find string example
+7k Golang : md5 hash of a string
+4.7k Golang : On lambda, anonymous, inline functions and function literals