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
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+8.6k Golang : Validate IPv6 example
+6.3k Golang : File system scanning
+8.9k Golang : Setting variable value with ldflags
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+17.9k Golang : Clearing slice
+17.6k Golang : Get host name or domain name from IP address
+5k Fontello : How to load and use fonts?
+6.9k Golang : Check from web if Go application is running or not
+8.9k Golang : Convert octal value to string to deal with leading zero problem
+9.1k Golang : Get login name from environment and prompt for password
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date