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
+20.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+21.4k Golang : Test file read write permission example
+10.5k Golang : Verify Linux user password again before executing a program example
+19.4k Golang : Convert PNG transparent background image to JPG or JPEG image
+23.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+7.8k Golang : Capture text return from exec function example
+9k Golang : Random Rune generator
+32.9k Golang : Integer is between a range
+11.4k Golang : Exit, terminating or aborting a program
+20.9k Golang : Read directory content with filepath.Walk()
+7.8k Golang : Heap sort example
+28.3k Golang : Get and Set User-Agent examples