पायथन स्ट्रिंग एनकोड () विधि
उदाहरण
UTF-8 स्ट्रिंग को एन्कोड करता है:
txt = "My name is Ståle"
x = txt.encode()
print(x)
परिभाषा और उपयोग
विधि निर्दिष्ट एन्कोडिंग का encode()
उपयोग करके स्ट्रिंग को एन्कोड करती है। यदि कोई एन्कोडिंग निर्दिष्ट नहीं है, तो UTF-8 का उपयोग किया जाएगा।
वाक्य - विन्यास
string.encode(encoding=encoding, errors=errors)
पैरामीटर मान
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:
|
और ज्यादा उदाहरण
उदाहरण
ये उदाहरण एएससीआई एन्कोडिंग का उपयोग करते हैं, और एक चरित्र जिसे एन्कोड नहीं किया जा सकता है, परिणाम विभिन्न त्रुटियों के साथ दिखा रहा है:
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"))