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
+15.5k Golang : invalid character ',' looking for beginning of value
+16.8k Golang : Get own process identifier
+8.1k Golang : HTTP Server Example
+9.8k Golang : Load ASN1 encoded DSA public key PEM file example
+7k Golang : constant 20013 overflows byte error message
+5k Google : Block or disable caching of your website content
+22.1k Golang : Join arrays or slices example
+34k Golang : Proper way to set function argument default value
+8.8k Golang : Get final balance from bit coin address example
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+6.3k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+10.4k Golang : Generate random integer or float number