×

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

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Python And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes 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.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 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 Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Implementing geometric shapes into the game in python

Geometric Drawings We are trying to draw various shapes of geometry by using pygame module to implement Geometric Drawings in game. Let us revise few syntax of basic shapes, to get started...

7 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.