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'

Updated on: 30-Sep-2019

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements