Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

numpy.defchararray.encode() in Python

  • Last Updated : 28 Nov, 2018

numpy.core.defchararray.encode(arr, encoding): This nuympy function encodes the string(object) based on the specified codec.

arr : array-like or string.
encoding : [str] Name of encoding being followed.
error : Specifying how to handle error.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Returns : Encoded string


# Python Program illustrating 
# numpy.char.encode() method 
import numpy as np 
arr1 = ['eAAAa', 'ttttds', 'AAtAAt']
arr2 = ['11sf', 'sdsf2', '1111f2']
# Printing the array
print ("\narr1 : ", arr1)
print ("\narr2 : ", arr2)
print ("\nEncoded arr1 : \n", np.char.encode(arr1, encoding ='cp037'))
print ("\nEncoded arr2 : \n", np.char.encode(arr2, encoding ='utf8'))
print ("\nEncoded arr2 : \n", np.char.encode(arr2, encoding ='utf8'
                                                 errors = 'strict'))


arr1 :  ['eAAAa', 'ttttds', 'AAtAAt']
arr2 :  ['11sf', 'sdsf2', '1111f2']

Encoded arr1 : 
 [b'\x85\xc1\xc1\xc1\x81' b'\xa3\xa3\xa3\xa3\x84\xa2'

Encoded arr2 : 
 [b'11sf' b'sdsf2' b'1111f2']

Encoded arr2 : 
 [b'11sf' b'sdsf2' b'1111f2']
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!