Python String encode() Yöntemi
Örnek
UTF-8 dizeyi kodlar:
txt = "My name is Ståle"
x = txt.encode()
print(x)
Tanım ve Kullanım
Yöntem encode()
, belirtilen kodlamayı kullanarak dizeyi kodlar. Kodlama belirtilmezse UTF-8 kullanılacaktır.
Sözdizimi
string.encode(encoding=encoding, errors=errors)
Parametre Değerleri
Parameter | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
encoding | Optional. A String specifying the encoding to use. Default is UTF-8 | ||||||||||||
errors | Optional. A String specifying the error method. Legal values are:
|
Daha fazla örnek
Örnek
Bu örnekler, sonucu farklı hatalarla gösteren ascii kodlamasını ve kodlanamayan bir karakter kullanır:
txt = "My name is Ståle"
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"))