NumPy包含以下函数, 用于对dtype字符串数组进行操作。
SN | Function | Description |
---|---|---|
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 srcmini', ' read python'] ))
输出
Concatenating two string arrays:['welcome to srcmini' '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("srcmini", 20, '*'))
输出
Padding the string through left and right with the fill char ******srcmini*****
numpy.char.capitalize()方法示例
import numpy as np print("Capitalizing the string using capitalize()...")print(np.char.capitalize("welcome to srcmini"))
输出
Capitalizing the string using capitalize()...Welcome to srcmini
numpy.char.title()方法示例
import numpy as np print("Converting string into title cased version...")print(np.char.title("welcome to srcmini"))
输出
Converting string into title cased version...Welcome To srcmini
numpy.char.lower()方法示例
import numpy as np print("Converting all the characters of the string into lowercase...")print(np.char.lower("WELCOME TO srcmini"))
输出
Converting all the characters of the string into lowercase...welcome to srcmini
numpy.char.upper()方法示例
import numpy as np print("Converting all the characters of the string into uppercase...")print(np.char.upper("Welcome To srcmini"))
输出
Converting all the characters of the string into uppercase...WELCOME TO srcmini
numpy.char.split()方法示例
import numpy as np print("Splitting the String word by word..")print(np.char.split("Welcome To srcmini"), sep = " ")
输出
Splitting the String word by word..['Welcome', 'To', 'srcmini']
numpy.char.splitlines()方法示例
import numpy as np print("Splitting the String line by line..")print(np.char.splitlines("Welcome\nTo\nsrcmini"))
输出
Splitting the String line by line..['Welcome', 'To', 'srcmini']
numpy.char.strip()方法示例
import numpy as np str = "welcome to srcmini"print("Original String:", str)print("Removing the leading and trailing whitespaces from the string")print(np.char.strip(str))
输出
Original String:welcome to srcminiRemoving the leading and trailing whitespaces from the stringwelcome to srcmini
numpy.char.join()方法示例
import numpy as np print(np.char.join(':', 'HM'))
输出
H:M
numpy.char.replace()方法示例
import numpy as npstr = "Welcome to srcmini"print("Original String:", str)print("Modified String:", end=" ")print(np.char.replace(str, "Welcome to", "www."))
输出
Original String: Welcome to srcminiModified String: www. srcmini
numpy.char.encode()和decode()方法示例
import numpy as npenstr = np.char.encode("welcome to srcmini", 'cp500')dstr =np.char.decode(enstr, 'cp500')print(enstr)print(dstr)
【NumPy字符串函数使用示例】输出
b'\xa6\x85\x93\x83\x96\x94\x85@\xa3\x96@\x91\x81\xa5\x81\xa3\x97\x96\x89\x95\xa3'welcome to srcmini
推荐阅读
- NumPy统计函数用法和示例
- NumPy Ndarray用法详细介绍(例子和配图)
- NumPy排序和搜索算法示例
- NumPy rint()方法
- Python中的numpy.reshape()例子
- numpy radians()方法
- Python中的NumPy矩阵乘法实现例子
- Numpy rad2deg()方法示例
- 如何修复Windows 10亮度不起作用(解决办法教程)