TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | encode() method with Examples

Python String encode() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String Encode() Method

Python encode() method encodes the string according to the provided encoding standard. By default Python strings are in unicode form but can be encoded to other standards also.

Encoding is a process of converting text from one standard code to another.

Signature

encode(encoding="utf-8", errors="strict")

Parameters

  • encoding : encoding standard, default is UTF-8
  • >
  • errors : errors mode to ignore or replace the error messages.

Both are optional. Default encoding is UTF-8.

Error parameter has a default value strict and allows other possible values 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' etc too.

Return Type

It returns an encoded string.

Let's see some examples to understand the encode() method.

Python String Encode() Method Example 1

A simple method which encode unicode string to utf-8 encoding standard.

# Python encode() function example
# Variable declaration
str = "HELLO"
encode = str.encode()
# Displaying result
print("Old value", str)
print("Encoded value", encode)

Output:

Old value HELLO
Encoded value b 'HELLO'

Python String Encode() Method Example 2

We are encoding a latin character

� into default encoding.
# Python encode() function example
# Variable declaration
str = "H�LLO"
encode = str.encode()
# Displaying result
print("Old value", str)
print("Encoded value", encode)

Output:

Old value H�LLO
Encoded value b'H\xc3\x8bLLO'

Python String Encode() Method Example 3

We are encoding latin character into ascii, it throws an error. See the example below

# Python encode() function example
# Variable declaration
str = "H�LLO"
encode = str.encode("ascii")
# Displaying result
print("Old value", str)
print("Encoded value", encode)

Output:

UnicodeEncodeError: 'ascii' codec can't encode character '\xcb' in position 1: ordinal not in range(128)

Python String Encode() Method Example 4

If we want to ignore errors, pass ignore as the second parameter.

# Python encode() function example
# Variable declaration
str = "H�LLO"
encode = str.encode("ascii","ignore")
# Displaying result
print("Old value", str)
print("Encoded value", encode)

Output:

Old value H�LLO
Encoded value b'HLLO'

Python String Encode() Method Example 5

It ignores error and replace character with ? mark.

# Python encode() function example
# Variable declaration
str = "H�LLO"
encode = str.encode("ascii","replace")
# Displaying result
print("Old value", str)
print("Encoded value", encode)

Output:

Old value H�LLO
Encoded value b'H?LLO'

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf