Reference Materials
Created with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python String Methods
- Python String capitalize()
- Python String center()
- Python String casefold()
- Python String count()
- Python String endswith()
- Python String expandtabs()
- Python String encode()
- Python String find()
- Python String format()
- Python String index()
- Python String isalnum()
- Python String isalpha()
- Python String isdecimal()
- Python String isdigit()
- Python String isidentifier()
- Python String islower()
- Python String isnumeric()
- Python String isprintable()
- Python String isspace()
- Python String istitle()
- Python String isupper()
- Python String join()
- Python String ljust()
- Python String rjust()
- Python String lower()
- Python String upper()
- Python String swapcase()
- Python String lstrip()
- Python String rstrip()
- Python String strip()
- Python String partition()
- Python String maketrans()
- Python String rpartition()
- Python String translate()
- Python String replace()
- Python String rfind()
- Python String rindex()
- Python String split()
- Python String rsplit()
- Python String splitlines()
- Python String startswith()
- Python String title()
- Python String zfill()
- Python String format_map()
Python String encode()
The encode() method returns an encoded version of the given string.
Example
title = 'Python Programming'
# change encoding to utf-8
print(title.encode())
# Output: b'Python Programming'
Syntax of String encode()
The syntax of encode() method is:
string.encode(encoding='UTF-8',errors='strict')
String encode() Parameters
By default, the encode() method doesn't require any parameters.
It returns an utf-8 encoded version of the string. In case of failure, it raises a UnicodeDecodeError exception.
However, it takes two parameters:
- encoding - the encoding type a string has to be encoded to
- errors - response when encoding fails. There are six types of error response
- strict - default response which raises a UnicodeDecodeError exception on failure
- ignore - ignores the unencodable unicode from the result
- replace - replaces the unencodable unicode to a question mark ?
- xmlcharrefreplace - inserts XML character reference instead of unencodable unicode
- backslashreplace - inserts a \uNNNN escape sequence instead of unencodable unicode
- namereplace - inserts a \N{...} escape sequence instead of unencodable unicode
Example 1: Encode to Default Utf-8 Encoding
# unicode string
string = 'pythön!'
# print string
print('The string is:', string)
# default encoding to utf-8
string_utf = string.encode()
# print result
print('The encoded version is:', string_utf)
Output
The string is: pythön! The encoded version is: b'pyth\xc3\xb6n!'
Example 2: Encoding with error parameter
# unicode string
string = 'pythön!'
# print string
print('The string is:', string)
# ignore error
print('The encoded version (with ignore) is:', string.encode("ascii", "ignore"))
# replace error
print('The encoded version (with replace) is:', string.encode("ascii", "replace"))
Output
The string is: pythön! The encoded version (with ignore) is: b'pythn!' The encoded version (with replace) is: b'pyth?n!'
Note: Try different encoding and error parameters as well.
String Encoding
Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points.
For efficient storage of these strings, the sequence of code points is converted into a set of bytes. The process is known as encoding.
There are various encodings present which treat a string differently. The popular encodings being utf-8, ascii, etc.
Using the string encode() method, you can convert unicode strings into any encodings supported by Python. By default, Python uses utf-8 encoding.
Also Read:
