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
+50.5k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+12.3k Golang : Arithmetic operation with numerical slices or arrays example
+33.7k Golang : Proper way to set function argument default value
+9.7k Golang : Check if user agent is a robot or crawler example
+9.4k Golang : Populate slice with sequential integers example
+7.4k Gogland : Where to put source code files in package directory for rookie
+20.5k PHP : Convert(cast) int to double/float
+4.8k Golang : Check if a word is countable or not
+5.2k Gogland : Datasource explorer
+31.2k Golang : How to convert(cast) string to IP address?
+25k Golang : Convert long hexadecimal with strconv.ParseUint example