Python string to bytes-Code Examples

Radu TM

Python string to bytes

Python by Radu TM June 16, 2022

# python string to bytes
s = 'Hello World'

# bytes()
b = bytes(s, encoding='utf-8')
print(b) # b'Hello World'

# decode()
s = b.decode(encoding='utf-8')
print(s) # Hello World

# encode()
b = s.encode(encoding='utf-8')
print(b) # b'Hello World'

0

20.181

Related Python Queries

Latest Python Queries

Explore code examples by categories