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
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+4.2k Swift : Convert (cast) Float to Int or Int32 value
+11k Golang : List running EC2 instances and descriptions
+7.4k Golang: Prevent over writing file with md5 hash
+18.2k Golang : How to run your code only once with sync.Once object
+21.2k Golang : Convert Unix timestamp to UTC timestamp
+12.1k Golang : Calculate elapsed years or months since a date
+4.3k Javascript : Change page title to get viewer attention
+4.9k PHP : Get client IP address
+25.7k Golang : Find files by name - cross platform example
+7k Golang : Randomize letters from a string example
+12.3k Golang : Verify token from Google Authenticator App