In the previous article, we have discussed Python Program for Pass Statement with Examples
String encode() Method in Python:
The encode() method encodes the string using the encoding specified. When no encoding is specified, UTF-8 is used.
Syntax:
string.encode(encoding=encoding, errors=errors)
Parameter Values:
encoding: This is Optional. A String indicating the encoding to be used. UTF-8 is the default.
errors: This is Optional. The error method is specified as a String. The following are legal values:
- backslashreplace – employs a backslash in place of the unencoded character
- ignore – does not encode characters that cannot be encoded.
- namereplace – replaces the character with text that describes the character.
- strict – On failure, the default raises an error.
- replace – inserts a question mark in place of the character.
- xmlcharrefreplace – a character is replaced with an xml character.
Examples:
Example1:
Input:
Given string = "Welcome to Pythön-prögrams"
Output:
b'Welcome to Pyth\\xf6n-pr\\xf6grams' b'Welcome to Pythn-prgrams' b'Welcome to Pyth\\N{LATIN SMALL LETTER O WITH DIAERESIS}n-pr\\N{LATIN SMALL LETTER O WITH DIAERESIS}grams' b'Welcome to Pyth?n-pr?grams' b'Welcome to Pythön-prögrams'
Example2:
Input:
Given string = "Hello this is vikråm"
Output:
b'Hello this is vikr\\xe5m' b'Hello this is vikrm' b'Hello this is vikr\\N{LATIN SMALL LETTER A WITH RING ABOVE}m' b'Hello this is vikr?m' b'Hello this is vikråm'
String encode() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
These examples demonstrate the use of ascii encoding and a character that cannot be encoded, with various errors:
Approach:
- Give the string as static input() and store it in a variable.
- Give the encoding value as ascii and errors as “backslashreplace” for the given string using the encode function and print it.
- Here “backslashreplace” employs a backslash in place of the unencoded character.
- Give the encoding value as ascii and errors as “ignore” for the given string using the encode function and print it.
- Here “ignore” does not encode characters that cannot be encoded.
- Give the encoding value as ascii and errors as “namereplace” for the given string using the encode function and print it.
- Here “namereplace” replaces the character with text that describes the character.
- Give the encoding value as ascii and errors as “replace” for the given string using the encode function and print it.
- Here “replace” inserts a question mark in place of the character.
- Give the encoding value as ascii and errors as “xmlcharrefreplace” for the given string using the encode function and print it.
- Here “xmlcharrefreplace” is replacing a character with an xml character.
- The Exit of the program.
Below is the implementation:
# Give the string as static input() and store it in a variable. gvn_str = "Welcome to Pythön-prögrams" # Give the encoding value as ascii and errors as "backslashreplace" for the # given string using the encode function and print it. # Here "backslashreplace" employs a backslash in place of the unencoded character. print(gvn_str.encode(encoding="ascii", errors="backslashreplace")) # Give the encoding value as ascii and errors as "ignore" for the given # string using the encode function and print it. # Here "ignore" does not encode characters that cannot be encoded. print(gvn_str.encode(encoding="ascii", errors="ignore")) # Give the encoding value as ascii and errors as "namereplace" for the given # string using the encode function and print it. # Here "namereplace" replaces the character with text that describes the character. print(gvn_str.encode(encoding="ascii", errors="namereplace")) # Give the encoding value as ascii and errors as "replace" for the given # string using the encode function and print it. # Here "replace" inserts a question mark in place of the character. print(gvn_str.encode(encoding="ascii", errors="replace")) # Give the encoding value as ascii and errors as "xmlcharrefreplace" for the # given string using the encode function and print it. # Here "xmlcharrefreplace" is replacing a character with an xml character. print(gvn_str.encode(encoding="ascii", errors="xmlcharrefreplace"))
Output:
b'Welcome to Pyth\\xf6n-pr\\xf6grams' b'Welcome to Pythn-prgrams' b'Welcome to Pyth\\N{LATIN SMALL LETTER O WITH DIAERESIS}n-pr\\N{LATIN SMALL LETTER O WITH DIAERESIS}grams' b'Welcome to Pyth?n-pr?grams' b'Welcome to Pythön-prögrams'
Method #2: UTF-8 encoding to the given string (Static Input)
Below is the implementation:
gvn_str = "Hello this is vikråm" rslt = gvn_str.encode() print(rslt)
Output:
b'Hello this is vikr\xc3\xa5m'
String Encoding:
Strings have been stored as Unicode since Python 3.0, which means that each character in the string is represented by a code point. As a result, each string is simply a series of Unicode code points.
The sequence of code points is converted into a set of bytes for efficient storage of these strings. Encoding is the name given to this process.
There are numerous encodings available, each of which treats a string differently. Popular encodings include utf-8, ascii, and others.
Using the string encode() method, you can convert unicode strings to any Python encoding. Python’s default encoding is utf-8.
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.
- Python String join() Function with Examples
- Python ljust() Function with Examples
- Python lstrip() Function with Examples
- Python Program for partition() Function with Examples