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
+7.4k Golang : Error reading timestamp with GORM or SQL driver
+7.1k Golang : Create zip/ePub file without compression(use Store algorithm)
+11.3k Golang : Concurrency and goroutine example
+17.3k Golang : Parse date string and convert to dd-mm-yyyy format
+32.5k Golang : How to check if a date is within certain range?
+15.2k Golang : Force download file example
+6.8k Golang : Squaring elements in array
+10.6k Golang : Natural string sorting example
+9.9k Golang : How to check if a website is served via HTTPS
+8.5k Golang : Find duplicate files with filepath.Walk
+7.9k Golang : HTTP Server Example
+7.6k Javascript : How to check a browser's Do Not Track status?