NumPy包含以下用于字符串数据类型数组操作的函数。
SN | 功能 | 描述 |
---|---|---|
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() | 使用编码解码的字符串进行逐个编码。 |
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']
import numpy as np
print("Printing a string multiple times:")
print(np.char.multiply("hello ",3))
输出:
Printing a string multiple times:
hello hello hello
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*****
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
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
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
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
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']
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']
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
import numpy as np
print(np.char.join(':','HM'))
输出:
H:M
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
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
本文链接:http://so.lmcjl.com/news/17529/