How to Return a Boolean Array True Where the String Array Ends with Suffix Using NumPy? Last Updated : 13 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to return a boolean array that is True where the string element in the array ends with a suffix using NumPy in Python. Example: Check the array ends with Com Input: [email protected] Output: True Input: [email protected] Output: False In Python, numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_. We can use Numpy.char.endswith method to return a Boolean array which is True where the string element in array ends with suffix. Syntax: char.endswith(a, suffix, start=0, end=None) Parameters a: array_like of str or unicodesuffix: strstart, end: int, optionalWith optional start, test beginning at that position. With optional end, stop comparing at that position. Returns out: ndarrayOutputs an array of bools.Example 1: We have an array of Email addresses, we need to check which of them are valid emails by checking the suffix ".com". If a string ends with ".com" it should return True otherwise False. Python3 # import required modules import numpy as np # initialising the array to be validated address_list = np.array(["[email protected]", "[email protected]", "[email protected]"]) # Calling the endswith method validated_array = np.char.endswith(address_list, suffix = ".com") print(validated_array) Output: [ True False True]Example 2: In the previous example, we have validated the email address with the suffix ".com", now we will validate the domain of the email address. This time we are interested in the domain "abc" only, not ".com" The address with domain abc should return True otherwise False. Assume that, there will be exactly 4 characters after the domain(ex: ".com") Python3 # import required modules import numpy as np # initialising the array to be validated address_list = np.array(["[email protected]", "[email protected]", "[email protected]"]) # Calling the endswith method # start = 0 : starts from the beginning of # a stringend = -4 : Ends at 4 places before # the string ending validated_array = np.char.endswith(address_list, suffix ="abc", start=0, end=-4) print(validated_array) Output: [ True True False] Comment More infoAdvertise with us Next Article How to Return a Boolean Array True Where the String Array Ends with Suffix Using NumPy? S sandeepburra Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads Return a boolean array which is True where the string element in array ends with suffix in Python In this article, we are going to see how we will return a boolean array which is True where the string element in the array ends with a suffix in Python. numpy.char.endswith() numpy.char.endswith() return True if the elements end with the given substring otherwise it will return False. Syntax : np.c 2 min read How to invert the elements of a boolean array in Python? Given a boolean array the task here is to invert its elements. A boolean array is an array which contains only boolean values like True or False, 1 or 0. Input : A=[true , true , false] Output: A= [false , false , true] Input: A=[0,1,0,1] Output: A=[1,0,1,0] Method 1: You can use simple if else me 2 min read How to split the element of a given NumPy array with spaces? To split the elements of a given array with spaces we will use numpy.char.split(). It is a function for doing string operations in NumPy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr. Parameters:arr : array_like of str or unicode.Input array 2 min read How to check whether specified values are present in NumPy array? Sometimes we need to test whether certain values are present in an array. Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the "in" operator. "in" operator is used to check whether certain element and values are present in a given sequence an 2 min read Find the length of each string element in the Numpy array NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). In this post 3 min read Like