 Open in App
Not now

# How to Return a Boolean Array True Where the String Array Ends with Suffix Using NumPy?

• Last Updated : 13 Jul, 2022

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: person_1@abc.com
Output: True
Input: person_3@xyz.co
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 unicode
• suffix: str
• start, end: int, optional
• With optional start, test beginning at that position. With optional end, stop comparing at that position.

Returns

• out: ndarray
• Outputs 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([``"person_1@abc.com"``,` `                         ``"person_2@abc.ccc"``,` `                         ``"person_3@xyz.com"``])`   `# 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([``"person_1@abc.com"``,` `                         ``"person_2@abc.ccc"``,` `                         ``"person_3@xyz.com"``])`   `# 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]`

My Personal Notes arrow_drop_up
Related Articles