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
+15.9k Golang : Get file permission
+4.8k Javascript : How to get width and height of a div?
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+7.4k Golang : Check to see if *File is a file or directory
+15.5k Golang : rune literal not terminated error
+33.6k Golang : How to check if slice or array is empty?
+10.1k Golang : How to get quoted string into another string?
+22.2k Golang : Convert seconds to minutes and remainder seconds
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+19.1k Mac OSX : Homebrew and Golang
+5.1k Golang : Experimental Jawi programming language