NumPy String Functions

In addition to NumPy's numerical capabilities, it also provides several functions that can be applied to strings represented in NumPy arrays.

For example, we can add two strings, change the contents of a string, case conversion, padding, trimming, and so on.


Common NumPy String Functions

Here are some of the string functions provided by NumPy:

Functions Descriptions
add() concatenates two strings
multiply() repeats a string for a specified number of times
capitalize() capitalizes the first letter of a string
lower() converts all uppercase characters in a string to lowercase
upper() converts all lowercase characters in a string to uppercase
join() joins a sequence of strings
equal() checks if two strings are equal or not

Note: These are some of the most commonly used NumPy string functions, please visit NumPy String Operations for more.


NumPy String add() Function

In NumPy, we use the np.char.add() function to perform element-wise string concatenation for two arrays of strings. For example,

import numpy as np

array1 = np.array(['iPhone: ', 'price: '])
array2 = np.array(['15', '$900'])

# perform element-wise array string concatenation
result = np.char.add(array1, array2)

print(result)

Output

['iPhone: 15' 'price: $900']

In this example, we have defined two NumPy arrays: array1 and array2, with string elements 'iphone: ' and 'price: ' and '15' and '$900' respectively.

We then used np.char.add(array1, array2) to concatenate the elements of array1 and array2 element-wise.

Finally, we have stored the concatenated string in result, which in our case are 'iPhone: 15' and 'price: $900'.


NumPy String multiply() Function

The np.char.multiply() function is used to perform element-wise string repetition. It returns an array of strings with each string element repeated a specified number of times.

Let's see an example.

import numpy as np  

# define array with three string elements
array1 = np.array(['A', 'B', 'C']) 

#  repeat each element in array1 two times 
result = np.char.multiply(array1, 2)  

print(result)  

# Output: ['AA' 'BB' 'CC']

Here, np.char.multiply(array1, 2) repeats each element in array1 two times.


NumPy String capitalize() Function

In NumPy, the np.char.capitalize() function is used to capitalize the first character of each string element in a given array. For example,

import numpy as np  

# define an array with three string elements
array1 = np.array(['eric', 'paul', 'sean'])  

# capitalize the first letter of each string in array1 
result = np.char.capitalize(array1)  

print(result) 

# Output: ['Eric' 'Paul' 'Sean']

In this example, np.char.capitalize(array1) capitalizes the first character of the string element in the array1 array.


NumPy String upper() and lower() Function

NumPy provides two string functions for converting the case of a string: np.char.upper() and np.char.lower().

Let's see an example.

import numpy as np

array1 = np.array(['nEpalI', 'AmeriCAN', 'CaNadIan'])

# convert all string elements to uppercase
result1 = np.char.upper(array1)

# convert all string elements to lowercase
result2 = np.char.lower(array1)

print("To Uppercase: ",result1)
print("To Lowercase: ",result2)

Output

To Uppercase:  ['NEPALI' 'AMERICAN' 'CANADIAN']
To Lowercase:  ['nepali' 'american' 'canadian']

Here,

  • np.char.upper(array1) - convert all strings in array1 to uppercase
  • np.char.lower(array1) - convert all strings in array1 to lowercase

NumPy String join() Function

In NumPy, we use the np.char.join() function to join strings in an array with a specified delimiter. For example,

import numpy as np

# create an array of strings
array1 = np.array(['hello', 'world'])

# join the strings in the array using a dash as the delimiter
result = np.char.join('-', array1)

print(result)

# Output: ['h-e-l-l-o' 'w-o-r-l-d']

In this example, the np.char.join('-', array1) function is used to join the strings in array1 using dash - as the delimiter.

The resulting strings have each character separated by a dash.


NumPy String equal() Function

The np.char.equal() function is used to perform element-wise string comparison between two string arrays.

Let's see an example.

import numpy as np

# create two arrays of strings
array1 = np.array(['C', 'Python', 'Swift'])
array2 = np.array(['C++', 'Python', 'Java'])

# check if each element of the arrays is equal
result = np.char.equal(array1, array2)

print(result)

# Output: [False True False]

Here, np.char.equal(array1, array2) checks whether the corresponding elements of array1 and array2 are equal or not.

The resulting boolean array [False True False] indicates that the first and third elements of array1 and array2 are not equal, while the second element is equal.