×

Python String encode() method

Python String encode() method

The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding

Syntax

String.encode([encoding[,errors]])

Parameter

Encoding: This parameter represents a String specifying the encoding to use and by default its value is UTF-8.

Errors: This parameter represents a String specifying the error method.

Return

This method returns an encoded version of the specified string.

Example 1

 # Python program explaining
 # the encode() method
 # unicode string
 str = 'Hellö World!'
 # printing the specified string
 print('The given string is:', str)
 # default encoding to utf-8
 str_utf = str.encode()
 # printing the encoded result
 print('The encoded result:', str_utf) 

Output

The given string is: Hellö World !
The encoded result: b'Hell\xc3\xb6 World !'

Example 2

 # Python String program explaining
 # the encode() method
 # initializing the string  
 Str = "This is my Python String example";
 # encoding the specified string
 Str = Str.encode('base64','strict');
 # printing the encoded string
 print ("Encoded String: " + Str)
 # decoding the encoded string
 print "Decoded String: " + Str.decode('base64','strict') 

Output

Encoded String: VGhpcyBpcyBteSBQeXRob24gU3RyaW5nIGV4YW1wbGU=
Decoded String: This is my Python String example

Example 3

 # Python program explaining
 # the encode() method
 # passing the unicode string
 string = 'Hellö Wörld!'
 # printing the specified string
 print('The given string:', string)
 # ignore error
 print('The encoded result with ignore:', string.encode("ascii", "ignore"))
 # replace error
 print('The encoded result with replace:', string.encode("ascii", "replace")) 

Output

The given string: Hellö Wörld!
 The encoded result with ignore: b'Hell Wrld!'
 The encoded result with replace: b'Hell? W?rld!' 

Related Topics

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.

Python memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.