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
+8.3k Golang : Delay or limit HTTP requests example
+3k Golang : Experimental Jawi programming language
+22k Golang : GORM read from database example
+3.1k Linux/MacOSX : Search and delete files by extension
+6.3k Golang : Emulate NumPy way of creating matrix example
+21k Golang : Time slice or date sort and reverse sort example
+23.6k PHP : Count number of JSON items/objects
+6k Golang : Multiplexer with net/http and map
+13.8k Golang : How to run your code only once with sync.Once object
+6.4k Golang : Routes multiplexer routing example with regular expression control
+14k Golang : How to make a file read only and set it to writable again?
+48.3k Golang : How to get time in milliseconds?