Numpy String Functions & Operations (original) (raw)

Last Updated : 23 Jan, 2025

NumPy String functions belong to the numpy.char module and are designed to perform element-wise operations on arrays. These functions can help to handle and manipulate string data efficiently.

Table of Content

In this article, we’ll explore the various string functions provided by NumPy along with their examples.

**String Operations

import numpy as np

converting to lowercase

print(np.char.lower(['GEEKS', 'FOR']))

converting to lowercase

print(np.char.lower('GEEKS'))

`

Output

['geeks' 'for'] geeks

import numpy as np

splitting a string

print(np.char.split('geeks for geeks'))

splitting a string

print(np.char.split('geeks, for, geeks', sep = ','))

`

Output

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks']

import numpy as np

splitting a string

print(np.char.join('-', 'geeks'))

splitting a string

print(np.char.join(['-', ':'], ['geeks', 'for']))

`

Output

g-e-e-k-s ['g-e-e-k-s' 'f:o:r']

FUNCTION DESCRIPTION
**numpy.strip() It is used to remove all the leading and trailing spaces from a string.
**numpy.capitalize() It converts the first character of a string to capital (uppercase) letter. If the string has its first character as capital, then it returns the original string.
**numpy.center() It creates and returns a new string which is padded with the specified character..
**numpy.decode() It is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme.
**numpy.encode() Returns the string in the encoded form
**numpy.ljust() Return an array with the elements of a left-justified in a string of length width.
**numpy.rjust() For each element in a, return a copy with the leading characters removed.
**numpy.strip() For each element in a, return a copy with the leading and trailing characters removed.
**numpy.lstrip() Convert angles from degrees to radians.
**numpy.rstrip() For each element in a, return a copy with the trailing characters removed.
**numpy.partition() Partition each element in a around sep.
**numpy.rpartition Partition (split) each element around the right-most separator.
**numpy.rsplit() For each element in a, return a list of the words in the string, using sep as the delimiter string.
**numpy.title() It is used to convert the first character in each word to Uppercase and remaining characters to Lowercase in string and returns new string.
**numpy.upper() Returns the uppercased string from the given string. It converts all lowercase characters to uppercase.If no lowercase characters exist, it returns the original string.

**String Information

import numpy as np

a=np.array(['geeks', 'for', 'geeks'])

counting a substring

print(np.char.count(a,'geek'))

counting a substring

print(np.char.count(a, 'fo'))

`

import numpy as np

a=np.array(['geeks', 'for', 'geeks'])

counting a substring

print(np.char.rfind(a,'geek'))

counting a substring

print(np.char.rfind(a, 'fo'))

`

Output

[ 0 -1 0] [-1 0 -1]

import numpy as np

counting a substring

print(np.char.isnumeric('geeks'))

counting a substring

print(np.char.isnumeric('12geeks'))

`

FUNCTION DESCRIPTION
**numpy.find() It returns the lowest index of the substring if it is found in given string. If its is not found then it returns -1.
**numpy.index() It returns the position of the first occurrence of substring in a string
**numpy.isalpha() It returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”.
**numpy.isdecimal() It returns true if all characters in a string are decimal. If all characters are not decimal then it returns false.
**numpy.isdigit() It returns “True” if all characters in the string are digits, Otherwise, It returns “False”.
**numpy.islower() It returns “True” if all characters in the string are lowercase, Otherwise, It returns “False”.
**numpy.isspace() Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
**numpy.istitle() Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
**numpy.isupper() Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
**numpy.rindex() Returns the highest index of the substring inside the string if substring is found. Otherwise it raises an exception.
**numpy.startswith() Returns True if a string starts with the given prefix otherwise returns False.

**String Comparison

import numpy as np

using equal() method

a=np.char.equal('geeks','for')

print(a)

`

import numpy as np

Comparing a string element-wise using not_equal() method

a = np.char.not_equal('geeks', 'for')

print(a)

`

**numpy.greater(): This function checks whether string1 is greater than string2 or not.

Python `

import numpy as np

comparing a string elementwise

a=np.char.greater('geeks','for')

print(a)

`

FUNCTION DESCRIPTION
**numpy.greater_equal() It checks whether string1 >= string2 or not.
**numpy.less_equal() It checks whether string1 is <= string2 or not.
**numpy.less() It check whether string1 is lesser than string2 or not.

Convenience class

**array: Creates a new NumPy array from a list or similar data structure.

Python `

import numpy as np arr = np.array([1, 2, 3, 4]) # Creating a NumPy array print(arr)

`

**chararray: Creates a NumPy array specialized for handling string operations.

Python `

import numpy as np char_arr = np.chararray((2, 3)) # Creating a 2x3 character array char_arr[:] = 'Hello' # Assigning string values print(char_arr)

`

**asarray: Converts input data into a NumPy array, avoiding unnecessary copying if already an array.

Python `

import numpy as np lst = [1, 2, 3, 4] arr = np.asarray(lst) # Converting list to NumPy array print(arr)

`

FUNCTIONS DESCRIPTION
chararray NumPy array for string operations, providing vectorized string processing capabilities.
array Creates a NumPy array from input data
asarray Converts input data into a NumPy array without copying

Similar Reads

Introduction







Creating NumPy Array













NumPy Array Manipulation


















Matrix in NumPy


















Operations on NumPy Array




Reshaping NumPy Array















Indexing NumPy Array






Arithmetic operations on NumPyArray










Linear Algebra in NumPy Array