Computer >> Computer tutorials >  >> Programming >> Python

How would you convert string to bytes in Python 3?


To convert string to bytes in Python 3, you can use the encode() function from the string class. For example,

>>> s = u"HellΘ WΘrld"
>>> s.encode('utf-8')
'Hell\xce\x98 W\xce\x98rld'