Python String encode() method

嚜燕ython String encode() method

Python String encode()

Python String encode() method returns a new string which is created from the given string, encoded using

specified encoding standard.

In this tutorial, we will learn the syntax and examples for encode() method of String class.

Syntax

The syntax of String encode() method in Python is

str.encode(encoding='utf-8', errors='strict')

where

Parameter

Required/Optional

Description

encoding

Optional

A string object. It specified the encoding standard to use.

errors

Optional

A string object. Specifies the action to take when an error occurs while

encoding.Following are the possible values for errors parameter, along

with what they do.1. 'backslashreplace' 每 uses a backslash instead of

the character that could not be encoded.2. 'ignore' 每 ignores the

characters that cannot be encoded.3. 'namereplace' 每 replaces the

character with a text explaining the character.4. 'strict' 每 Default,

raises an error on failure.5. 'replace' 每 replaces the character with a

question mark.6. 'xmlcharrefreplace' 每 replaces the character with an

xml character.

Example

In this example, we will take a string, and encode it using &utf-16*.

Python Program

x = 'abc'

result = x.encode(encoding='utf-16')

print(result)

Output

b'\xff\xfea\x00b\x00c\x00'

Conclusion

In this Python Tutorial, we learned how to encode a string with specific encoding using String method 每

encode().

Python Programming

?

Python Tutorial

?

Install Python

?

Install Anaconda Python

?

Python HelloWorld Program

?

Python Variables

?

Python Datatype Conversion

?

Python Comments

?

Python If

?

Python If Else

?

Python While Loop

?

Python For Loop

?

Python Operators

?

Python Functions

?

Python Lambda Functions

?

Python Builtin Functions

Python Collections

?

Python Strings

?

Python Lists

?

Python Tuples

?

Python Dictionary

?

Python Sets

Libraries

?

Python Numpy Tutorial

?

Python SciPy Tutorial

?

Python Pandas Tutorial

?

Python Matplotlib Tutorial

Advanced Topics

?

Python Multithreading

Useful Resources

?

Python Interview Questions

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download