String Functions & Operations
0 1860
Introduction
NumPy, the backbone of numerical computing in Python, includes a dedicated set of functions for handling string data. These string operations are found under thenumpy.char module and are designed to work element-wise on string arrays, making them highly efficient for batch text processing.
1. Combining Strings using add()
The numpy.char.add() function merges strings element-wise from two arrays. It’s a clean alternative to list comprehensions for concatenating array elements.
import numpy as np
arr1 = np.array(['Data', 'Machine'])
arr2 = np.array(['Science', 'Learning'])
result = np.char.add(arr1, arr2)
print(result) # ['DataScience' 'MachineLearning']
2. Repeating Text with multiply()
numpy.char.multiply() repeats each string in the array a specified number of times. Ideal for generating repeated patterns or filler content.
arr = np.array(['Hi', 'No'])
print(np.char.multiply(arr, 2)) # ['HiHi' 'NoNo']
3. Case Conversion: lower() and upper()
Standardize string cases using lower() for lowercase and upper() for uppercase conversions. Both operate element-wise on arrays.
arr = np.array(['Python', 'NumPy'])
print(np.char.lower(arr)) # ['python' 'numpy']
print(np.char.upper(arr)) # ['PYTHON' 'NUMPY']
4. Splitting and Joining Strings
Usesplit() to break strings based on a delimiter, and join() to insert a separator between characters of each string.
arr = np.array(['apple,banana', 'red,blue'])
print(np.char.split(arr, ',')) # [['apple', 'banana'], ['red', 'blue']]
print(np.char.join('-', arr)) # ['a-p-p-l-e-,-b-a-n-a-n-a' 'r-e-d-,-b-l-u-e']
5. Cleaning Strings with strip()
Remove unnecessary spaces or characters using strip(). It trims from both ends of each string.
arr = np.array([' hello ', ' world! '])
print(np.char.strip(arr)) # ['hello' 'world!']
6. Formatting with capitalize()
Capitalize the first character of each string and lowercase the rest using capitalize(). Handy for titles and proper nouns.
arr = np.array(['python', 'NUMPY'])
print(np.char.capitalize(arr)) # ['Python' 'Numpy']
7. Character Checks with isalpha() and isdigit()
Evaluate each string to determine if it contains only alphabetic or numeric characters.
arr = np.array(['Test', '1234', 'Hello1'])
print(np.char.isalpha(arr)) # [ True False False]
print(np.char.isdigit(arr)) # [False True False]
8. Toggling Case using swapcase()
swapcase() inverts the case of each character. Uppercase becomes lowercase and vice versa.
arr = np.array(['Hello', 'WORLD'])
print(np.char.swapcase(arr)) # ['hELLO' 'world']
Conclusion
NumPy’s string functions offer a versatile toolkit for array-based text operations. Whether you're cleaning data, preparing input for machine learning, or just formatting strings, these tools can make the process more efficient and Pythonic.
Share:



Comments
Waiting for your comments