Home >>Python String Methods >Python String encode() Method
Python string encode() method is used to encode the string using the specified encoding. If no encoding is specified then UTF-8 will be used for the encoding.
Syntax:string.encode(encoding=encoding, errors=errors)
Parameter | Description |
---|---|
encoding | This is an optional parameter. It defines a String specifying the encoding to use. |
errors | This is an optional parameter. It defines a String specifying the error method. |
txt = "My name is Jerry"
x = txt.encode()
print(x)
txt = "Welcome to PHPTPÖÑT"
print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))