How to Convert Bytes to String in Python?

python

To convert bytes to string in Python, you can use the decode() method.

Example:

# bytes variable
b = b'Hello, World!'

# convert bytes to string
s = b.decode()

# print string
print(s)      # output: Hello, World!

In the above example, the bytes variable b is converted to a string using the decode() method. The resulting string is stored in a new variable named s, which is then printed to the console.

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?