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
+6.7k Generate Random number with math/rand in Go
+4.7k Golang : How to get ECDSA curve and parameters data?
+2.5k Python : Convert(cast) string to bytes example
+2.4k Golang : Reverse text lines or flip line order example
+2.3k Golang : Find the longest line of text example
+11.3k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+1.3k MariaDB/MySQL : How to get version information
+8.1k SSL : The certificate is not trusted because no issuer chain was provided
+5.1k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+3.1k Golang : When to use make or new?
+3.6k Golang : Scan files for certain pattern and rename part of the files
+9.7k Swift : Convert (cast) Int to String ?