numpy.strings.slice#

strings.slice(a,start=None,stop=<novalue>,step=None,/)[source]#

Slice the strings ina by slices specified bystart,stop,step.Like in the regular Pythonslice object, if onlystart isspecified then it is interpreted as thestop.

Parameters:
aarray-like, withStringDType,bytes_, orstr_ dtype

Input array

startNone, an integer or an array of integers

The start of the slice, broadcasted toa’s shape

stopNone, an integer or an array of integers

The end of the slice, broadcasted toa’s shape

stepNone, an integer or an array of integers

The step for the slice, broadcasted toa’s shape

Returns:
outndarray

Output array ofStringDType,bytes_ orstr_ dtype,depending on input type

Examples

>>>importnumpyasnp>>>a=np.array(['hello','world'])>>>np.strings.slice(a,2)array(['he', 'wo'], dtype='<U5')
>>>np.strings.slice(a,2,None)array(['llo', 'rld'], dtype='<U5')
>>>np.strings.slice(a,1,5,2)array(['el', 'ol'], dtype='<U5')

One can specify different start/stop/step for different array entries:

>>>np.strings.slice(a,np.array([1,2]),np.array([4,5]))array(['ell', 'rld'], dtype='<U5')

Negative slices have the same meaning as in regular Python:

>>>b=np.array(['hello world','γεια σου κόσμε','你好世界','👋 🌍'],...dtype=np.dtypes.StringDType())>>>np.strings.slice(b,-2)array(['hello wor', 'γεια σου κόσ', '你好', '👋'], dtype=StringDType())
>>>np.strings.slice(b,-2,None)array(['ld', 'με', '世界', ' 🌍'], dtype=StringDType())
>>>np.strings.slice(b,[3,-10,2,-3],[-1,-2,-1,3])array(['lo worl', ' σου κόσ', '世', '👋 🌍'], dtype=StringDType())
>>>np.strings.slice(b,None,None,-1)array(['dlrow olleh', 'εμσόκ υοσ αιεγ', '界世好你', '🌍 👋'],      dtype=StringDType())
On this page