Overview:
-
The numpy.strings.startswith() function checks whether the string elements of an ndarray start with a given prefix. If a string element starts with the given prefix the result is True. The result is False otherwise. The results are returned as an ndarray containing Boolean values. If the input parameter is a string then a single Boolean value is returned.
-
The supported string types are: bytes, _str, StrDType.
Example 1 - Finding prefixes in an ndarray:
# Example Python program that finds whether the elements of an ndarray # Create a NumPy array of unicode strings stockSymbols[0][0] = "NAS_MSFT" # Mark the NASDAQ symbols with True # Print symbols and their prefix presence print("Symbols start with NAS:") |
Output:
Symbols: [['NAS_MSFT' 'NAS_NVDA'] ['NAS_AAPL' 'HKG_4335']] Symbols start with NAS: [[ True True] [ True False]] |
Example 2 - Finding prefixes in an ndarray using start and stop positions:
# Example Python program that finds prefixes stockSymbols = numpy.ndarray(shape = (2, 3), dtype = numpy.dtype('U10')) stockSymbols[0][0] = "EXC1_EQY_ABC" stockSymbols[1][0] = "EXC1_FUT_ABC" prefix = "EQY" print("Symbols belong to equity segment:") |
Output:
[[ True True True] [False False False]] |
Example 3 - Find prefix on a string parameter:
# Example Python program that checks a Python string for prefix using inputString = "Long long ago there was a..." # Using NumPy # Using Python's str class |
Output:
True True |