NumPy String Functions
NumPy is not only for numbers — it also provides powerful tools for working with strings and text data.
Using NumPy string functions, you can easily perform operations on entire arrays of text without writing loops.
These functions are available under
np.char.
What are NumPy String Functions?
NumPy string functions allow you to perform vectorized string operations on arrays.
Instead of processing each string one by one, NumPy applies operations to the entire array at once.
Why Use NumPy String Functions?
- Fast string processing
- No loops required
- Works on large datasets
- Useful in data cleaning
- Essential for text preprocessing in ML
1. Converting to Uppercase
import numpy as np
arr = np.array(["python", "numpy", "data"])
print(np.char.upper(arr))
Output
['PYTHON' 'NUMPY' 'DATA']
2. Converting to Lowercase
arr = np.array(["PYTHON", "NUMPY", "DATA"])
print(np.char.lower(arr))
Output
['python' 'numpy' 'data']
3. Capitalize Strings
arr = np.array(["python", "numpy", "data science"])
print(np.char.capitalize(arr))
Output
['Python' 'Numpy' 'Data science']
4. Title Case
arr = np.array(["python programming", "data science"])
print(np.char.title(arr))
Output
['Python Programming' 'Data Science']
5. String Concatenation
arr1 = np.array(["Hello", "Good"])
arr2 = np.array(["World", "Morning"])
print(np.char.add(arr1, arr2))
Output
['HelloWorld' 'GoodMorning']
6. Adding Separator in Concatenation
arr1 = np.array(["Hello", "Good"])
arr2 = np.array(["World", "Morning"])
print(np.char.add(arr1, " " + arr2))
Output
['Hello World' 'Good Morning']
7. String Replace
arr = np.array(["I love Java", "Java is powerful"])
print(np.char.replace(arr, "Java", "Python"))
Output
['I love Python' 'Python is powerful']
8. Splitting Strings
arr = np.array(["Python NumPy", "Data Science"])
print(np.char.split(arr))
Output
[list(['Python', 'NumPy']) list(['Data', 'Science'])]
9. Joining Strings
arr = np.array([["Python", "NumPy"], ["Data", "Science"]])
print(np.char.join(" ", arr))
Output
['Python NumPy' 'Data Science']
10. String Length
arr = np.array(["Python", "NumPy", "AI"])
print(np.char.str_len(arr))
Output
[6 5 2]
11. Stripping Spaces
arr = np.array([" python ", " numpy "])
print(np.char.strip(arr))
Output
['python' 'numpy']
12. Checking Start With
arr = np.array(["python", "numpy", "pandas"])
print(np.char.startswith(arr, "p"))
Output
[ True False True]
13. Checking End With
arr = np.array(["data.csv", "image.png", "file.txt"])
print(np.char.endswith(arr, ".csv"))
Output
[ True False False]
Real-World Example: Usernames Cleanup
arr = np.array([" Alice ", "BOB", "charlie "])
cleaned = np.char.strip(arr)
print(np.char.capitalize(cleaned))
Output
['Alice' 'Bob' 'Charlie']
Real-World Example: Product Labels
arr = np.array(["laptop pro", "gaming mouse"])
print(np.char.title(arr))
Output
['Laptop Pro' 'Gaming Mouse']
NumPy String Function Table
| Function | Purpose |
|---|---|
upper() | Convert to uppercase |
lower() | Convert to lowercase |
capitalize() | Capitalize first letter |
title() | Title case |
add() | Concatenate strings |
replace() | Replace text |
split() | Split strings |
join() | Join strings |
str_len() | String length |
strip() | Remove spaces |
Advantages of NumPy String Functions
- Fast vectorized operations
- No loops required
- Works on large datasets
- Easy data cleaning
- Useful in NLP preprocessing
Summary
NumPy string functions (via np.char) allow efficient text processing on arrays. They support case conversion, splitting, joining, replacing, and many other operations.
These functions are part of NumPy and are widely used in text processing and AI applications built with Python.
Conclusion
Mastering NumPy string functions helps you efficiently clean and process text data, making them essential for data science, machine learning, and natural language processing tasks.


0 Comments