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
+10.5k Get form post value in Go
+10.4k Golang : Fuzzy string search or approximate string matching example
+7.3k How to show different content from website server when AdBlock is detected?
+6.2k Golang : Validate credit card example
+4.2k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+12.2k Golang : Generate Code128 barcode
+4.8k List of Golang XML tutorials
+16.3k Golang : Clone with pointer and modify value
+21.5k Golang : simulate tail -f or read last line from log file example
+4.6k Golang : Stop goroutine without channel
+10.9k Golang : Print UTF-8 fonts on image example
+4.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents