
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - String Functions
String 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.
Key Features of NumPy String Functions
Following are the key features of NumPy String Functions −
- Element-wise Operations: The core advantage of NumPy string functions is their ability to perform operations on each element of an array independently. This allows for efficient manipulation of large datasets.
- Vectorization: By using NumPy library operations are vectorized i.e., which enhances performance compared to traditional Python string handling methods. Vectorization utilizes optimized C libraries to perform computations by reducing the execution time significantly.
- Compatibility with Arrays: NumPy string functions work directly with arrays of strings by making it easier to process large amounts of textual data without needing to convert them into lists or other formats.
The String operations are performed element-wise on arrays. They are particularly useful for low-level data manipulation and efficient computation.
List of String Functions
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.ljust
Left-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.isdigit
Checks 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
The add() function in NumPy is used to concatenate strings using the + operator as shown in the example below −
a = "Hello" b = "World" result = a + " " + b print(result)
Following is the output obtained −
Hello World
The multiply() Function
The multiply() function in NumPy is used to multiply(repeat) strings using the * operator as shown in the example below −
a = "Hello" result = a * 3 print(result)
This will produce the following result −
HelloHelloHello
The center() Function
The center() function centers a string in a field of a specified width, padding it with spaces or a specified character −
s = "hello" result = s.center(10, '*') print(result)
Following is the output of the above code −
**hello***
The capitalize() Function
The capitalize() function capitalizes the first character of the string and makes all other characters lowercase −
s = "hello world" result = s.capitalize() print(result)
The output obtained is as shown below −
Hello world
The title() Function
The title() function capitalizes the first letter of each word in the string −
s = "hello world" result = s.title() print(result)
After executing the above code, we get the following output −
Hello World
The lower() and upper() Functions
The lower() function converts all characters in the string to lowercase. Whereas, the upper() function converts all characters in the string to uppercase −
s = "Hello World" res1 = s.lower() res2 = s.upper() print("Lowercase:", res1) print("Uppercase:",res2)
The result produced is as follows −
Lowercase: hello world Uppercase: HELLO WORLD
The decode() Function
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 −
# Bytes object b = b"hello world" result = b.decode('utf-8') print(result)
We get the output as shown below −
hello world