Python String encode() Method
You are Here:
Python String encode()
The encode()
method returns an encoded version of the string.
Default encoding is 'utf-8'.
Example
Python Compiler
txt = "Learn pythön!"
x = txt.encode()
print(x)
Output
b'Learn pyth\xc3\xb6n!'
Syntax
txt.encode(encoding, errors)
Parameter Values
Value | Type | Explanation |
---|---|---|
encoding | Optional | Specifies the encoding to use. Default value is 'utf-8'. |
errors | Optional | Specifies the error method. Possible values are:
|
Return Value
Value | Explanation |
---|---|
Number | Returns an encoded version of the string. |
More Example
In the following example, we use "ascii" as an encoding value and all types of errors as an error method.
Example
Python Compiler
txt = "Learn pythön!"
print(txt.encode("ascii", "backslashreplace"))
print(txt.encode("ascii", "ignore"))
print(txt.encode("ascii", "namereplace"))
print(txt.encode("ascii", "replace"))
print(txt.encode("ascii", "xmlcharrefreplace"))
Output
b'Learn pyth\\xf6n!'
b'Learn pythn!'
b'Learn pyth\\N{LATIN SMALL LETTER O WITH DIAERESIS}n!'
b'Learn pyth?n!'
b'Learn pythön!'
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.
We are working to cover every Single Concept in Python.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.