Python : Convert(cast) bytes to string example

Tags : python string bytes

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"
 <class 'bytes'>
 var2 = str(var)
 <class 'str'>

You can convert the bytes variable straightaway by adding the 'str' pre-fix.

 var = str(b"this is a string")
 <class 'str'>

Happy Coding!

  See also : Python : Convert(cast) string to bytes example

Tags : python string bytes

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.

Helping out