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 : How to get own program name during runtime ?
+16.2k Golang : Get sub string example
+11.3k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+8.5k Golang : Implementing class(object-oriented programming style)
+10.9k Golang : Natural string sorting example
+7.9k Golang : Generate human readable password
+31.3k Golang : Calculate percentage change of two values
+21.3k Golang : Get password from console input without echo or masked
+11.4k Golang : How to use if, eq and print properly in html template
+9.1k Golang : Go as a script or running go with shebang/hashbang style
+4.6k Java : Generate multiplication table example
+41.1k Golang : How to check if a string contains another sub-string?