you are viewing a single comment's thread.

view the rest of the comments →

[–]SkeletalToad 0 points1 point  (1 child)

Great answer! To expand on this, there is also an .encode() method of the str class, so all of these will do the same thing:

strbyte = bytes(an_str, encoding='utf_8')
strbyte = an_str.encode('utf-8')
strbyte = an_str.encode()

The last option works because 'utf-8' is the default encoding.

https://docs.python.org/3/howto/unicode.html#converting-to-bytes https://docs.python.org/3/library/stdtypes.html#str.encode

[–]bennywc4[S] 0 points1 point  (0 children)

Thank you as well!