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
+4.7k Unix/Linux : Get reboot history or check when was the last reboot date
+7.6k Golang : Gorilla web tool kit schema example
+7.7k Golang : Capture text return from exec function example
+2.1k Golang : Switch Redis database redis.NewClient
+8.9k Golang : Print how to use flag for your application example
+14.5k Golang : Update database with GORM example
+12.8k Golang : How to determine if user agent is a mobile device example
+48.4k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+12k Swift : Convert (cast) Int to String ?
+9.4k Golang : Get UDP client IP address and differentiate clients by port number
+12.7k Elastic Search : Mapping date format and sort by date
+27.8k Golang : JQuery AJAX post data to server and send data back to client example