NumPy Encode Function

PythonPythonBeginner
Practice Now

Introduction

NumPy is a popular Python library used for working with arrays. One of the submodules of NumPy is the char module which provides several string operations that can be applied on NumPy arrays. In this lab tutorial, we will discuss the encode() function of the char module of NumPy. We will learn how to use this function to encode a given input string. We will cover the syntax, parameters and returned values of encode() function using examples.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/lists -.-> lab-86433{{"`NumPy Encode Function`"}} python/tuples -.-> lab-86433{{"`NumPy Encode Function`"}} python/importing_modules -.-> lab-86433{{"`NumPy Encode Function`"}} python/numerical_computing -.-> lab-86433{{"`NumPy Encode Function`"}} python/build_in_functions -.-> lab-86433{{"`NumPy Encode Function`"}} numpy/bool_idx -.-> lab-86433{{"`NumPy Encode Function`"}} numpy/fancy_idx -.-> lab-86433{{"`NumPy Encode Function`"}} end

Import NumPy library

We begin by importing NumPy library which is required for using encode() function. Following is the code block for importing NumPy:

import numpy as np

Use encode() function with different encoding schemes

We will now use the encode() function with two different encoding schemes 'cp037' and 'utf-8'. For this, we first define an array of strings.

a = ['aAaAaA', '  aA  ', 'abBABba','dffgs','ttsred']

Using encoding scheme 'cp037':

We use encode() function with encoding scheme 'cp037' by passing the input array and encoding type as parameters. Following is the code block:

x = np.char.encode(a, encoding='cp037', errors=None)

The above code block reads the input array a and encodes it using 'cp037' encoding scheme. The encoded string is stored in variable x. Since no error handling mechanism is specified, errors parameter is set to None. We now print the input array and encoded string as follows:

print("Input is:")
print(a)

print("Encoded String is:")
print(x)

Output:

Input is:
['aAaAaA', '  aA  ', 'abBABba', 'dffgs', 'ttsred']
Encoded String is:
[b'\x81\xc1\x81\xc1\x81\xc1' b'@@\x81\xc1@@'
 b'\x81\x82\xc2\xc1\xc2\x82\x81' b'\x84\x86\x86\x87\xa2'
 b'\xa3\xa3\xa2\x99\x85\x84']

Using encoding scheme 'utf-8':

We use encode() function with encoding scheme 'utf-8' by passing the input array and encoding type as parameters. Following is the code block:

x = np.char.encode(a, encoding='utf-8', errors=None)

The above code block reads the input array a and encodes it using 'utf-8' encoding scheme. The encoded string is stored in variable x. Since no error handling mechanism is specified, errors parameter is set to None. We now print the input array and encoded string as follows:

print("Input is:")
print(a)

print("Encoded String is:")
print(x)

Output:

Input is:
['aAaAaA', '  aA  ', 'abBABba', 'dffgs', 'ttsred']
Encoded String is:
[b'aAaAaA' b' aA ' b'abBABba' b'dffgs' b'ttsred']

Summary

In this lab, we learned about the encode() function of the char module of NumPy library. We covered how it is used with its syntax and values returned by this function. We also discussed different encoding schemes such as 'cp037' and 'utf-8' and demonstrated their usage with examples. The encode() function allows encoding of an input string in an element-wise manner and returns the encoded string.

Other Python Tutorials you may like