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
+12.4k Golang : Print UTF-8 fonts on image example
+6.2k Golang : How to write backslash in string?
+8.7k Golang : Set or add headers for many or different handlers
+18.2k Golang : Convert IPv4 address to decimal number(base 10) or integer
+22.8k Golang : Strings to lowercase and uppercase example
+8.3k Golang : Check from web if Go application is running or not
+16.6k Golang : Convert slice to array
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+14.7k Golang : Convert(cast) int to float example
+6.4k Golang : Extract sub-strings
+9.6k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases