Signup/Sign In
LAST UPDATED: DECEMBER 10, 2020

NumPy startswith() function

In this tutorial, we will cover startswith() function of the char module in the Numpy library.

The startswith() function returns a boolean array with values that can either be True or False. This function will return True, if the given string starts with the specified prefix value in the function. Hence the function stands true to its name.

This function calls str.startswith ifor all the string elements of the array, if it is used with an array.

Syntax of startswith():

The syntax required to use this function is as follows:

numpy.char.startswith(a, prefix, start=0, end=None)

The above syntax indicates that isspace() function takes 4 parameters.

In the above syntax, the argument 'a' is mainly used to indicate the input array of the string on which this function will be applied.

Parameters:

Let us now discuss the parameters of this function:

  • a
    This parameter indicates an array either of strings or Unicode.

  • prefix
    This parameter indicates the prefix value that is checked in all the strings.

  • start, end
    Both these parameters are optional. With the optional start parameter, the search begins from this position, and with the optional end parameter the function stop comparing at that position.

Returned Values:

This function returns an array of boolean values.

Example 1:

Let us check the output of this function if the prefix does exist in the input string:

import numpy as np

arr = "The Grass is greener on the other side always"
print("The Input:\n",arr)
prefix = 'Th'

x = np.char.startswith(arr, prefix, start = 0, end = None)
print("The output is :")
print (x)


The Input:
The Grass is greener on the other side always
The output is :
True

Example 2:

Let us check the output of this function if the prefix does not exist in the input string:

import numpy as np

arr = "The Grass is greener on the other side always"
print("The Input:\n",arr)
prefix = 'It'

x = np.char.startswith(arr, prefix, start = 0, end = None)
print("The output is :")
print (x)


The Input:
The Grass is greener on the other side always
The output is :
False

Summary

In this tutorial we learned about startswith() function of the Numpy library. We covered how it is used with its syntax and values returned by this function. There are a few examples in order to gain an understanding of the working of this function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.