NumPy教程-NumPy字符串函数
NumPy包含以下用于操作字符串数据类型数组的函数。
| 序号 | 函数 | 描述 |
|---|---|---|
| 1 | add() | 用于连接相应的数组元素(字符串)。 |
| 2 | multiply() | 返回指定字符串的多个副本,例如,如果字符串'hello'乘以3,则返回字符串'hello hello'。 |
| 3 | center() | 返回字符串的副本,其中原始字符串在左右填充填充字符的指定数量后居中。 |
| 4 | capitalize() | 返回原始字符串的副本,其中原始字符串的第一个字母转换为大写。 |
| 5 | title() | 返回字符串的标题化版本,即字符串中的每个单词的第一个字母转换为大写。 |
| 6 | lower() | 返回字符串的副本,其中所有字母转换为小写。 |
| 7 | upper() | 返回字符串的副本,其中所有字母转换为大写。 |
| 9 | split() | 返回字符串中的单词列表。 |
| 9 | splitlines() | 返回字符串中的行列表,按行边界分隔。 |
| 10 | strip() | 返回删除了前导和尾随空格的字符串的副本。 |
| 11 | join() | 返回一个字符串,它是给定序列中所有字符串连接而成的。 |
| 12 | replace() | 返回一个副本,其中所有出现的特定子字符串都被指定的子字符串替换。 |
| 13 | decode() | 用于逐元素地使用指定的编解码器对指定字符串进行解码。 |
| 14 | encode() | 用于逐元素地对解码后的字符串进行编码。 |
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 javatpointnumpy.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 Javatpointnumpy.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 javatpointnumpy.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 JAVATPOINTnumpy.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 javatpointnumpy.char.join() 方法示例
import numpy as np
print(np.char.join(':','HM')) 输出:
H:Mnumpy.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. Javatpointnumpy.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