English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy String Functions

Usage of NumPy string functions

These functions are defined in the character array class (numpy.char).

FunctionDescription
add()Concatenate each string element of two arrays sequentially.
multiply()Return the string after multiple concatenations of elements.
center()Center the string.
capitalize()Convert the first letter of the string to uppercase.
title()Convert the first letter of each word in the string to uppercase.
lower()Convert the array elements to lowercase.
upper()Convert the array elements to uppercase.
split()Split the string with a specified separator and return an array list.
splitlines()Return the line list in the element, split by newline characters.
strip()Remove the specified characters from the beginning or end of the element.
join()Concatenate the elements of the array with a specified separator.
replace()Replace all substrings in the string with a new string.
decode()The array elements are called str.decode sequentially.
encode()The array elements are called str.encode sequentially.

numpy.char.add()

The numpy.char.add() function concatenates the elements of two arrays sequentially.

 Example
 print('Concatenating two strings:', np.char.add(['hello'], ['w']))3codebox))
 print('\n')
  
 print('Connection example:', np.char.add(['hello', 'hi'], ['numpy', 'w']))3codebox))

You can use encoders from the standard Python library.

 Concatenate two strings:
 ['hello w3codebox'
 Concatenation example:
 ['hello numpy' 'hi w3codebox'

numpy.char.multiply()

The numpy.char.multiply() function performs multiple concatenations.

 Example 
  
 print (np.char.multiply('wow,w3codebox! ',5))

You can use encoders from the standard Python library.

wow,w3codebox!wow,w3codebox!wow,w3codebox!wow,w3codebox!wow,w3codebox!

numpy.char.center()

The numpy.char.center() function is used to center a string and fill it with the specified character on both sides.

 Example 
  
 # np.char.center(str, width, fillchar):
 # str: string, width: length, fillchar: fill character
 print (np.char.center('w3codebox', 30, fillchar = '.'

You can use encoders from the standard Python library.

   ...........w3codebox............

numpy.char.capitalize()

Example 
print (np.char.capitalize('w')),3codebox'))

You can use encoders from the standard Python library.

w3codebox

numpy.char.title()

The numpy.char.title() function capitalizes the first letter of each word in a string:

Example 
print (np.char.title('hello w3codebox, i like you.')

You can use encoders from the standard Python library.

Hello w3codebox, I Like You.

numpy.char.lower()

The numpy.char.lower() function converts each element of the array to lowercase. It calls str.lower for each element.

Example 
 
print (np.char.lower(['LIDIHUO','WEBSITE']))
  
# Operate strings
print (np.char.lower('LIDIHUO'))

You can use encoders from the standard Python library.

 ['w3codebox' website'
 w3codebox

numpy.char.upper()

The numpy.char.upper() function converts each element of the array to uppercase. It calls str.upper for each element.

Example 

3codebox
# Operate strings
print(np.char.upper('w3codebox'))

You can use encoders from the standard Python library.

 ['LIDIHUO' 'WEBSITE']
 LIDIHUO

numpy.char.split()

The numpy.char.split() function splits strings by a specified delimiter and returns an array. By default, the delimiter is a space.

Example 
# Default separator is space
print(np.char.split('hello w'3codebox i like you
# Separator is '.'
print(np.char.split('www.w'3codebox.com', sep = '.'))

You can use encoders from the standard Python library.

 ['hello', 'w3codebox', 'i', 'like', 'you'
 ['www', 'w3codebox', 'com'

numpy.char.splitlines()

The numpy.char.splitlines() function splits strings by newline characters as delimiters and returns an array.

Example 
print(np.char.splitlines('i\nlike w3codebox')) 
print(np.char.splitlines('i\rlike w3codebox'))

You can use encoders from the standard Python library.

 ['i', 'like w3codebox'
 ['i', 'like w3codebox'

\n, \r, \r\n can be used as newline characters.

numpy.char.strip()

The numpy.char.strip() function is used to remove specific characters at the beginning or end of the string.

Example 
# Remove 'a' characters from the beginning and end of a string
print(np.char.strip('aaaa abbb acccca', 'a'))
  
# Remove 'a' characters from the beginning and end of array elements
print(np.char.strip(['aaaa', 'abbb', 'cccca'], 'a'))

You can use encoders from the standard Python library.

 aaaa abbb acccc
 ['aaa' 'bbb' 'ccc']

numpy.char.join()

The numpy.char.join() function connects elements or strings in an array by specifying a delimiter

Example 
# Operate strings
print(np.char.join(':', 'w'3codebox'))
  
# Operate array elements with multiple delimiters
print (np.char.join([':','-print(np.char.join([':','}}3[['w

You can use encoders from the standard Python library.

 codebox','google'))
 n:h:o:o:o-['n:h:o:o:o' 'g-['n:h:o:o:o' 'g-o-g-l

e'

numpy.char.replace()

Example 
The numpy.char.replace() function replaces all substrings in the string with a new string.3print(np.char.replace('i like w

You can use encoders from the standard Python library.

codebox', 'nh', 'aa'))

i like aaooo

numpy.char.encode()-8The numpy.char.encode() function calls the str.encode() function for each element in the array. The default encoding is utf

Example 
import numpy as np3a = np.char.encode('w5codebox', 'cp 
00')

You can use encoders from the standard Python library.

b'\x00'))88\x96\x96\x96'

Output Result is:

numpy.char.decode()

Example 
 import numpy as np3a = np.char.encode('w5codebox', 'cp 
 00')
 print(a)5print(np.char.decode(a,'cp
 b'\x00'))88\x96\x96\x96'
 w3codebox