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
+17.1k Golang : Covert map/slice/array to JSON or XML format
+28.7k Get file path of temporary file in Go
+15k Golang : Submit web forms without browser by http.PostForm example
+19.2k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+6.7k Golang : When to use make or new?
+7k Golang : Calculate BMI and risk category
+5.4k Golang : Pad file extension automagically
+5.3k PHP : Hide PHP version information from curl
+6.3k PHP : Get client IP address
+8k Golang : Trim everything onward after a word
+6.1k PageSpeed : Clear or flush cache on web server
+32.3k Golang : Validate email address with regular expression