📜  NumPy 字符串

📅  最后修改于: 2020-10-27 04:12:32             🧑  作者: Mango

NumPy字符串函数

NumPy包含以下函数,用于对dtype 字符串数组进行操作。

SN Function Description
1 add() It is used to concatenate the corresponding array elements (strings).
2 multiply() It returns the multiple copies of the specified string, i.e., if a string ‘hello’ is multiplied by 3 then, a string ‘hello hello’ is returned.
3 center() It returns the copy of the string where the original string is centered with the left and right padding filled with the specified number of fill characters.
4 capitalize() It returns a copy of the original string in which the first letter of the original string is converted to the Upper Case.
5 title() It returns the title cased version of the string, i.e., the first letter of each word of the string is converted into the upper case.
6 lower() It returns a copy of the string in which all the letters are converted into the lower case.
7 upper() It returns a copy of the string in which all the letters are converted into the upper case.
9 split() It returns a list of words in the string.
9 splitlines() It returns the list of lines in the string, breaking at line boundaries.
10 strip() Returns a copy of the string with the leading and trailing white spaces removed.
11 join() It returns a string which is the concatenation of all the strings specified in the given sequence.
12 replace() It returns a copy of the string by replacing all occurrences of a particular substring with the specified one.
13 decode() It is used to decode the specified string element-wise using the specified codec.
14 encode() It is used to encode the decoded string element-wise.

numpy.char.add()方法示例

import numpy as np 
print("Concatenating two string arrays:")
print(np.char.add(['welcome','Hi'], [' to Javatpoint', ' read python'] ))

输出:

Concatenating two string arrays:
['welcome to Javatpoint' 'Hi read python']

numpy.char.multiply()方法示例

import numpy as np 
print("Printing a string multiple times:")
print(np.char.multiply("hello ",3))

输出:

Printing a string multiple times:
hello hello hello 

numpy.char.center()方法示例

import numpy as np 
print("Padding the string through left and right with the fill char *");
#np.char.center(string, width, fillchar)
print(np.char.center("Javatpoint", 20, '*'))

输出:

Padding the string through left and right with the fill char *
*****Javatpoint*****

numpy.char.capitalize()方法示例

import numpy as np 
print("Capitalizing the string using capitalize()...")
print(np.char.capitalize("welcome to javatpoint"))

输出:

Capitalizing the string using capitalize()...
Welcome to javatpoint

numpy.char.title()方法示例

import numpy as np 
print("Converting string into title cased version...")
print(np.char.title("welcome to javatpoint"))

输出:

Converting string into title cased version...
Welcome To Javatpoint

numpy.char.lower()方法示例

import numpy as np 
print("Converting all the characters of the string into lowercase...")
print(np.char.lower("WELCOME TO JAVATPOINT"))

输出:

Converting all the characters of the string into lowercase...
welcome to javatpoint

numpy.char.upper()方法示例

import numpy as np 
print("Converting all the characters of the string into uppercase...")
print(np.char.upper("Welcome To Javatpoint"))

输出:

Converting all the characters of the string into uppercase...
WELCOME TO JAVATPOINT

numpy.char.split()方法示例

import numpy as np 
print("Splitting the String word by word..")
print(np.char.split("Welcome To Javatpoint"),sep = " ")

输出:

Splitting the String word by word..
['Welcome', 'To', 'Javatpoint']

numpy.char.splitlines()方法示例

import numpy as np 
print("Splitting the String line by line..")
print(np.char.splitlines("Welcome\nTo\nJavatpoint"))

输出:

Splitting the String line by line..
['Welcome', 'To', 'Javatpoint']

numpy.char.strip()方法示例

import numpy as np 
str = "     welcome to javatpoint     "
print("Original String:",str)
print("Removing the leading and trailing whitespaces from the string")
print(np.char.strip(str))

输出:

Original String:      welcome to javatpoint     
Removing the leading and trailing whitespaces from the string
welcome to javatpoint

numpy.char.join()方法示例

import numpy as np 
print(np.char.join(':','HM'))

输出:

H:M

numpy.char.replace()方法示例

import numpy as np
str = "Welcome to Javatpoint"
print("Original String:",str)
print("Modified String:",end=" ")
print(np.char.replace(str, "Welcome to","www."))

输出:

Original String: Welcome to Javatpoint
Modified String: www. Javatpoint

numpy.char.encode()和decode()方法示例

import numpy as np
enstr = np.char.encode("welcome to javatpoint", 'cp500')
dstr =np.char.decode(enstr, 'cp500')
print(enstr)
print(dstr)

输出:

b'\xa6\x85\x93\x83\x96\x94\x85@\xa3\x96@\x91\x81\xa5\x81\xa3\x97\x96\x89\x95\xa3'
welcome to javatpoint