Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy String Functions

NumPy String Functions

The following functions perform vectorized string operations on arrays whose dtype is numpy.string_ or numpy.unicode_. They are based on standard string functions in Python's built-in library.

These functions are defined in the character array class (numpy.char).

function Description
add() Concatenate string elements of two arrays element by element
multiply() Return strings after element-wise multiple concatenation
center() Center strings
capitalize() Convert the first letter of a string to uppercase
title() Convert the first letter of each word in a string to uppercase
lower() Convert array elements to lowercase
upper() Convert array elements to uppercase
split() Split strings by a specified separator and return a list of arrays
splitlines() Return a list of lines in the elements, split by newline characters
strip() Remove specific characters at the beginning or end of elements
join() Join elements in an array by a specified separator
replace() Replace all substrings in a string with a new string
decode() Call on array elements in sequencestr.decode
encode() Call on array elements in sequencestr.encode

numpy.char.add()

The numpy.char.add() function concatenates the elements of two arrays as strings, element by element.

Example

import

numpy

as

np

print

(

'

Concatenate two strings:

'

)

print

(

np

.

char

.

add

(

[

'

hello

'

]

,

[

'

xyz

'

]

)

)

print

(

'

\n

'

)

print

(

'

Concatenation example:

'

)

print

(

np

.

char

.

add

(

[

'

hello

'

,

'

hi

'

]

,

[

'

abc

'

,

'

xyz

'

]

)

)

The output is:

连接两个字符串:

['hello xyz']



Connection example:

['hello abc' 'hi xyz']

numpy.char.multiply()

The numpy.char.multiply() function performs multiple concatenation.

Example

import

numpy

as

np

print

(

np

.

char

.

multiply

(

'

Runoob

'

,

3

)

)

The output is:

Runoob Runoob Runoob 

numpy.char.center()

The numpy.char.center() function centers a string and pads it on the left and right with a specified character.

Example

import

numpy

as

np

np.char.center(str , width,fillchar) :

str: string, width: length, fillchar: fill character

print

(

np

.

char

.

center

(

'

Runoob

'

,

20

,

fillchar

=

'

'

)

)

The output is:

*******Runoob*******

numpy.char.capitalize()

The numpy.char.capitalize() function converts the first letter of a string to uppercase:

Example

import

numpy

as

np

print

(

np

.

char

.

capitalize

(

'

runoob

'

)

)

The output is:

Runoob

numpy.char.title()

The numpy.char.title() function converts the first letter of each word in a string to uppercase:

Example

import

numpy

as

np

print

(

np

.

char

.

title

(

'

i like runoob

'

)

)

The output is:

I Like Runoob

numpy.char.lower()

The numpy.char.lower() function converts each element of an array to lowercase. It calls str.lower on each element.

Example

import

numpy

as

np

# operate on array

print

(

np

.

char

.

lower

(

[

'

RUNOOB

'

,

'

GOOGLE

'

]

)

)

Operate on strings

print

(

np

.

char

.

lower

(

'

RUNOOB

'

)

)

The output is:

['runoob' 'google']

runoob

numpy.char.upper()

The numpy.char.upper() function converts each element of an array to uppercase. It calls str.upper on each element.

Example

import

numpy

as

np

# operate on array

print

(

np

.

char

.

upper

(

[

'

runoob

'

,

'

google

'

]

)

)

Operate on strings

print

(

np

.

char

.

upper

(

'

runoob

'

)

)

The output is:

['RUNOOB' 'GOOGLE']

RUNOOB

numpy.char.split()

numpy.char.split() splits strings by a specified separator and returns an array. By default, the separator is a space.

Example

import

numpy

as

np

The separator defaults to a space

print

(

np

.

char

.

split

(

'

i like runoob?

'

)

)

The separator is .

print

(

np

.

char

.

split

(

'

www.runoob.com

'

,

sep

=

'

.

'

)

)

The output is:

['i', 'like', 'runoob?']

['www', 'runoob', 'com']

numpy.char.splitlines()

The numpy.char.splitlines() function splits strings using newline characters as separators and returns an array.

Example

import

numpy

as

np

Newline character \n

print

(

np

.

char

.

splitlines

(

'

i

\n

like runoob?

'

)

)

print

(

np

.

char

.

splitlines

(

'

i

\r

like runoob?

'

)

)

The output is:

['i', 'like runoob?']

['i', 'like runoob?']

\n, \r, \r\n can all be used as newline characters.

numpy.char.strip()

The numpy.char.strip() function removes specific characters at the beginning or end.

Example

import

numpy

as

np

Remove the a character from the beginning and end of a string

print

(

np

.

char

.

strip

(

'

ashok arunooba

'

,

'

a

'

)

)

Remove the a character from the beginning and end of array elements

print

(

np

.

char

.

strip

(

[

'

arunooba

'

,

'

admin

'

,

'

java

'

]

,

'

a

'

)

)

The output is:

shok arunoob

['runoob' 'dmin' 'jav']

numpy.char.join()

The numpy.char.join() function joins elements in an array or strings by a specified separator

Example

import

numpy

as

np

Operate on strings

print

(

np

.

char

.

join

(

'

:

'

,

'

runoob

'

)

)

Specify multiple separators to operate on array elements

print

(

np

.

char

.

join

(

[

'

:

'

,

'

'

]

,

[

'

runoob

'

,

'

google

'

]

)

)

The output is:

r:u:n:o:o:b

['r:u:n:o:o:b' 'g-o-o-g-l-e']

numpy.char.replace()

The numpy.char.replace() function replaces all substrings in a string with a new string.

Example

import

numpy

as

np

print

(

np

.

char

.

replace

(

'

i like runoob

'

,

'

oo

'

,

'

cc

'

)

)

The output is:

i like runccb

numpy.char.encode()

The numpy.char.encode() function calls the str.encode function on each element in an array. The default encoding is utf-8, and codecs from the standard Python library can be used.

Example

import

numpy

as

np

a

=

np

.

char

.

encode

(

'

runoob

'

,

'

cp500

'

)

print

(

a

)

The output is:

b'\x99\xa4\x95\x96\x96\x82'

numpy.char.decode()

The numpy.char.decode() function decodes encoded elements with str.decode().

Example

import

numpy

as

np

a

=

np

.

char

.

encode

(

'

runoob

'

,

'

cp500

'

)

print

(

a

)

print

(

np

.

char

.

decode

(

a

,

'

cp500

'

)

)

The output is:

b'\x99\xa4\x95\x96\x96\x82'

runoob