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
+28.3k Golang : Connect to database (MySQL/MariaDB) server
+17.7k Golang : Clone with pointer and modify value
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+8.3k How to show different content from website server when AdBlock is detected?
+11.1k How to test Facebook App on localhost ?
+10k Golang : Sort and reverse sort a slice of integers
+21.3k Golang : Get password from console input without echo or masked
+18.3k Golang : Get command line arguments
+6.3k Apt-get to install and uninstall Golang
+5.4k Golang : Generate Interleaved 2 inch by 5 inch barcode
+8.7k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+7.6k Golang : Detect sample rate, channels or latency with PortAudio