PyDocs

Nav

Python Runtime Services

str.encode

builtins

method of str
  • str.encode(encoding='utf-8', errors='strict')
Encode the string using the codec registered for encoding.

encoding
  The encoding in which to encode the string.
errors
  The error handling scheme to use for encoding errors.
  The default is 'strict' meaning that encoding errors raise a
  UnicodeEncodeError.  Other possible values are 'ignore', 'replace'
  and 'xmlcharrefreplace' as well as any other name registered with
  codecs.register_error that can handle UnicodeEncodeErrors.
Changed in 3.1, 3.9
  • 3.1: Added support for keyword arguments.
  • 3.9: The value of the errors argument is now checked in Python Development Mode and in debug mode.
1 Example
>>> encoded_str_to_bytes = 'Python'.encode()
>>> type(encoded_str_to_bytes)
<class 'bytes'>
>>> encoded_str_to_bytes
b'Python'
from the Python docs
See Also
No see-alsos for builtins/str.encode
0 Notes
No notes for str.encode