Table of Contents
ToggleString functions in NumPy are designed to operate on arrays of strings. They are part of the NumPy char module, which provides a set of vectorized string operations that can be applied to each element of a string array.
Following are the key features of NumPy String Functions −
The String operations are performed element-wise on arrays. They are particularly useful for low-level data manipulation and efficient computation.
The following functions are used to perform vectorized string operations for arrays of dtype numpy.string_ or numpy.unicode_. They are based on the standard string functions in Python’s built-in library.
| Sr.No. | Operation & Description |
|---|---|
| 1 | numpy.char.add()Concatenates two arrays of strings element-wise. |
| 2 | numpy.char.center()Centers each string in an array within a specified width, padded with a specified character. |
| 3 | numpy.char.capitalize()Capitalizes the first character of each string in the array. |
| 4 | numpy.char.decode()Decodes each string in an array using the specified encoding. |
| 5 | numpy.char.encode()Encodes each string in an array using the specified encoding. |
| 6 | numpy.char.ljustLeft-justifies each string in an array, padding with a specified character. |
| 7 | numpy.char.lower()Converts all characters of each string in the array to lowercase. |
| 8 | numpy.char.lstrip()Strips leading characters from each string in an array. |
| 9 | numpy.char.mod()Formats strings using specified values for placeholders in the strings. |
| 10 | numpy.char.multiply()Repeats each string in the array a specified number of times. |
| 11 | numpy.char.replace()Replaces occurrences of a substring with another substring in each string. |
| 12 | numpy.char.rjust()Right-justifies each string in an array, padding with a specified character. |
| 13 | numpy.char.rstrip()Strips trailing characters from each string in an array. |
| 14 | numpy.char.strip()Strips leading and trailing characters from each string in an array. |
| 15 | numpy.char.swapcase()Swaps the case of each character in each string. |
| 16 | numpy.char.title()Converts each string in the array to title case. |
| 17 | numpy.char.translate()Translates characters in each string according to a translation table. |
| 18 | numpy.char.upper()Converts all characters of each string in the array to uppercase. |
| 19 | numpy.char.zfill()Pads each string with zeros on the left to fill a specified width. |
| 20 | numpy.char.equal()Compares each string in an array for equality with another array. |
| 21 | numpy.char.not_equal()Compares each string in an array for inequality with another array. |
| 22 | numpy.char.greater_equal()Compares each string in an array to see if it is greater than or equal to another. |
| 23 | numpy.char.less_equal()Compares each string in an array to see if it is less than or equal to another. |
| 24 | numpy.char.greater()Compares each string in an array to see if it is greater than another. |
| 25 | numpy.char.less()Compares each string in an array to see if it is less than another. |
| 26 | numpy.char.count()Counts occurrences of a substring in each string in the array. |
| 27 | numpy.char.endswith()Checks if each string in the array ends with a specified suffix. |
| 28 | numpy.char.find()Finds the lowest index of a substring in each string. |
| 29 | numpy.char.index()Similar to find, but raises an error if the substring is not found. |
| 30 | numpy.char.isalnum()Checks if each string is alphanumeric. |
| 31 | numpy.char.isalpha()Checks if each string is alphabetic. |
| 32 | numpy.char.isdecimal()Checks if each string is a decimal string. |
| 33 | numpy.char.isdigitChecks if each string contains only digits. |
| 34 | numpy.char.islower()Checks if each string is in lowercase. |
| 35 | numpy.char.isnumeric()Checks if each string is numeric. |
| 36 | numpy.char.isspace()Checks if each string contains only whitespace. |
| 37 | numpy.char.istitle()Checks if each string is title-cased. |
| 38 | numpy.char.isupper()Checks if each string is in uppercase. |
| 39 | numpy.char.rfind()Finds the highest index of a substring in each string. |
| 40 | numpy.char.rindex()Similar to rfind, but raises an error if the substring is not found. |
| 41 | numpy.char.startswith()Checks if each string starts with a specified prefix. |
| 42 | numpy.char.str_len()Returns the length of each string in the array. |
| 43 | numpy.char.split()Returns the splitted array string. |
| 44 | numpy.char.splitlines()Split each element of an array of strings into a list of lines. |
| 45 | numpy.char.join()Join the elements of an array of strings with a specified delimiter. |
Let’s look at the important functions quickly −
The add() function in NumPy is used to concatenate strings using the + operator as shown in the example below −
# Open Compiler a = "Hello" b = "World" result = a + " " + b print(result)
Output:
Following is the output obtained −
Hello World
The multiply() function in NumPy is used to multiply(repeat) strings using the * operator as shown in the example below −
# Open Compiler a = "Hello" result = a * 3 print(result)
Output:
This will produce the following result −
HelloHelloHello
The center() function centers a string in a field of a specified width, padding it with spaces or a specified character −
# Open Compiler s = "hello" result = s.center(10, '*') print(result)
Output:
Following is the output of the above code −
**hello***
The capitalize() function capitalizes the first character of the string and makes all other characters lowercase −
# Open Compiler s = "hello world" result = s.capitalize() print(result)
Output:
The output obtained is as shown below −
Hello world
The title() function capitalizes the first letter of each word in the string −
# Open Compiler s = "hello world" result = s.title() print(result)
Output:
After executing the above code, we get the following output −
Hello World
The lower() function converts all characters in the string to lowercase. Whereas, the upper() function converts all characters in the string to uppercase −
# Open Compiler
s = "Hello World"
res1 = s.lower()
res2 = s.upper()
print("Lowercase:", res1)
print("Uppercase:",res2)
Output:
The result produced is as follows −
Lowercase: hello world
Uppercase: HELLO WORLD
In Python 3, the decode() function is typically used for byte objects, not strings. To decode bytes to a string, you use the decode() function −
# Open Compiler
# Bytes object
b = b"hello world"
result = b.decode('utf-8')
print(result)
Output:
We get the output as shown below −
hello world
Key Takeaway: Master string functions in NumPy with Vista Academy!
