w3resource

NumPy String: numpy.char.startswith() function

numpy.char.startswith() function

The numpy.char.startswith() method returns an array of boolean values which indicates whether the elements in a given string array start with a particular prefix or not.

Syntax:

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

Parameters:

Name Description Required /
Optional
a: array_like of str or unicode Input an array_like of string or unicode. Required
prefix: str Input string or unicode Required
start, end: int With optional start, test beginning at that position. With optional end, stop comparing at that position. Optional

Return value:

Array of booleans.

Example: Using numpy.char.startswith() to check for the presence of a substring at the start of a string

>>> import numpy as np
>>> x = np.char.startswith('dishonoest man', 'dis', start=0, end=None)
>>> x
array(True)

In the above example, the function returns True because the first three characters of the string 'dishonest man' match the substring 'dis'. The start and end parameters can be used to specify a subset of the string to search for the substring.

Pictorial Presentation:

NumPy String operation: startswith() function

Example: Checking if string starts with a prefix

>>> import numpy as np
>>> y = np.char.startswith('honoest man', 'prefix', start=0, end=None)
>>> y
array(False)

In the above example, the code checks whether the string 'honoest man' starts with the prefix 'prefix'. Since the string does not start with the prefix 'prefix', the function returns a boolean value of False.

Pictorial Presentation:

NumPy String operation: startswith() function

Python - NumPy Code Editor:

Previous: rindex()
Next: chararray()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/string-operations/startswith.php