What are the different string functions of NumPy?
NumPy string functions are special tools that help us work with text data in Python. Just like we have tools for math problems, we also have tools for dealing with text, and that's what these functions are. They can do things like adding text together (like "Hello" and "World" becoming "HelloWorld"), making all letters in text lower or uppercase (like "HELLO" becoming "hello"), or replacing a part of the text with something else (like changing "bad" to "good" in "bad apple" to get "good apple"). These functions are very handy when we have lots of text data to handle, and the best part is, they are designed to work super fast even with thousands or millions of pieces of text.Here learn about the differet types of string functions available in Numpy with examples.
Important String Functions in NumPy
import numpy as np
# Original strings
str1 = "Hello"
str2 = "World"
# Using np.char.add()
print("np.char.add: ", np.char.add(str1, str2))
#output np.char.add: HelloWorld
# Using np.char.multiply()
print("np.char.multiply: ", np.char.multiply(str1, 3))
#output np.char.multiply: HelloHelloHello
# Using np.char.center()
print("np.char.center: ", np.char.center(str1, 20, fillchar = '*'))
# outout np.char.center: *******Hello********
# Using np.char.capitalize()
print("np.char.capitalize: ", np.char.capitalize('hello world'))
#output np.char.capitalize: Hello world
# Using np.char.title()
print("np.char.title: ", np.char.title('hello world'))
#output np.char.title: Hello World
# Using np.char.lower()
print("np.char.lower: ", np.char.lower('HELLO'))
#output np.char.lower: hello
# Using np.char.upper()
print("np.char.upper: ", np.char.upper('hello'))
#output np.char.upper: HELLO
# Using np.char.split()
print("np.char.split: ", np.char.split ('hello how are you?', sep =' '))
#output np.char.split: ['hello', 'how', 'are', 'you?'
# Using np.char.strip()
print("np.char.strip: ", np.char.strip(' hello world '))
#output np.char.strip: hello world
# Using np.char.join()
print("np.char.join: ", np.char.join(':','dmy'))
#output np.char.join: d:m:y
# Using np.char.replace()
print("np.char.replace: ", np.char.replace ('He is a good dancer', 'is', 'was'))
#output np.char.replace: He was a good dancer
List of NumPy String Functions
NumPy string functions with their definitions:
| Function | Description |
np.char.add() |
Performs element-wise string concatenation. |
np.char.multiply() |
Returns the string repeated specified number of times. |
np.char.center() |
Returns a copy of the given string with elements centered in a string of specified length. |
np.char.capitalize() |
Capitalizes the first letter of the string. |
np.char.title() |
Returns the element-wise title cased version of the string or unicode. |
np.char.lower() |
Converts all uppercase characters in a string to lowercase and returns the string. |
np.char.upper() |
Converts all lowercase characters in a string to uppercase and returns the string. |
np.char.split() |
Returns a list of words in the string, using specified delimiter string. |
np.char.splitlines() |
Returns a list of lines in the element, breaking at line boundaries. |
np.char.strip() |
Returns a copy with the leading and trailing characters removed. |
np.char.join() |
Returns a string which is the concatenation of the strings in the sequence. |
np.char.replace() |
Returns a copy of the string with all occurrences of a substring replaced by a new substring. |
np.char.encode() |
Calls str.encode element-wise. |
np.char.decode() |
Calls bytes.decode element-wise. |
np.char.startswith() |
Tests if the start of each string element matches a pattern. |
np.char.endswith() |
Tests if the end of each string element matches a pattern. |
np.char.count() |
Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end]. |
np.char.find() |
Returns the lowest index in the string where substring sub is found. |
np.char.index() |
Like find, but raise ValueError when the substring is not found. |
np.char.isnumeric() |
Checks whether all characters in each string in the array are numeric. |
np.char.isdecimal() |
Checks whether all characters in each string in the array are decimal. |
np.char.isdigit() |
Checks whether all characters in each string in the array are digits. |
np.char.islower() |
Returns a boolean array which is True where all characters in each string in the array are lowercase. |
np.char.isupper() |
Returns a boolean array which is True where all characters in each string in the array are uppercase. |
np.char.istitle() |
Returns a boolean array which is True for each element if the element is a titlecased string. |
np.char.isspace() |
Returns a boolean array which is True where all characters in each string in the array are spaces. |
np.char.isalpha() |
Returns a boolean array which is True for each element if all characters in the string are alphabetic. |
np.char.isalnum() |
Returns a boolean array which is True for each element if all characters in the string are alphanumeric. |
np.char.zfill() |
Returns the numeric string left-filled with zeros. |
np.char.ljust() |
Returns the string left-justified in a string of specified length. |
np.char.rjust() |
Returns the string right-justified in a string of specified length. |
np.char.equal() |
Performs element-wise string comparison. |
np.char.notequal() |
Returns a boolean array with a True entry where corresponding strings are not equal. |
np.char.less() |
Performs element-wise string comparison, returning True where string1 is lexicographically less than string2. |
np.char.less_equal() |
Performs element-wise string comparison, returning True where string1 is lexicographically less than or equal to string2. |
np.char.greater() |
Performs element-wise string comparison, returning True where string1 is lexicographically greater than string2. |
np.char.greater_equal() |
Performs element-wise string comparison, returning True where string1 is lexicographically greater than or equal to string2. |