Python numpy 字符串函数

Python numpy 字符串函数用于根据您的要求修改给定的字符串。Numpy 字符串函数包括:add、multiply、capitalize、title、upper、lower、center、split、splitlines、strip、join、replace、encode 和 decode。例如,numpy 字符串 upper 函数将其转换为大写。这些示例将帮助您理解字符串函数。

Python numpy add (添加)

python numpy add 函数用于字符串连接。在此示例中,我们使用此 numpy add 函数来添加两个字符串

import numpy as np

print('Numpy String concatenation')
print(np.char.add(['Tutorial'], ['Gateway']))

numpy add 函数执行字符串连接的输出

Numpy String concatenation
['TutorialGateway']

这次我们声明了两个不同的字符串。接下来,我们使用了这个 char add 函数

import numpy as np
 
fname = 'Suresh'
company = ' Tutorial Gateway'
 
print(np.char.add([fname], [company]))

char add 函数的输出。

['Suresh Tutorial Gateway']

在这里,我们连接了一个字符串变量和示例文本

import numpy as np
 
fname = 'Suresh'
 
print('concatenation')
print(np.char.add([fname], [' Working at Tutorial Gateway']))

char add 输出

concatenation
['Suresh Working at Tutorial Gateway']

Python numpy multiply (乘法/重复)

multiply 函数用于将给定的字符串乘以或重复指定的次数。在此示例中,p.char.multiply(‘hello ‘, 2)将 hello 重复两次。

import numpy as np
 
print('---Numpy String multiply---')
print(np.char.multiply('hello ', 2))
 
print('-------')
 
fname = 'Numpy '
print(np.char.multiply(fname, 4))
print(np.char.multiply(fname, 6))
Python Numpy multiply Function

Python numpy capitalize (首字母大写)

capitalize 函数用于将整个字符串中的第一个字母大写。

import numpy as np
 
print('---Numpy String capitalize---')
print(np.char.capitalize('hello'))
print(np.char.capitalize('hello world'))
 
print('-------')
 
fname = 'numpy'
company = 'tutorial gateway'
print(np.char.capitalize(fname))
print(np.char.capitalize(company))
Python Numpy capitalize Method

Python numpy title (标题)

title 函数用于将每个单词的第一个字母转换为大写。

import numpy as np
 
print('---Numpy String title---')
print(np.char.title('hello'))
print(np.char.title('hello world'))
print(np.char.title('hi, how are you?'))
 
print('-------')
 
name = 'python'
company = 'tutorial gateway'
print(np.char.title(name))
print(np.char.title(company))
Numpy title Function

Python numpy upper (大写)

upper 将字符串中的所有字母转换为大写

import numpy as np

print('---String upper---')
print(np.char.upper('hello'))
print(np.char.upper('hello world'))
print(np.char.upper('hi, how are you?'))
print('\n---Uppercase example 2----')
 
name = 'python'
company = 'tutorial gateway'
print(np.char.upper(name))
print(np.char.upper(company))
Numpy upper Function

Python numpy lower (小写)

lower 将字符串中的所有字符转换为小写

import numpy as np
 
print('---String lower---')
print(np.char.lower('HI, HOW DO YOU DO?'))
print(np.char.lower('HELLO WORLD'))
print(np.char.lower('WELCOME'))
 
print('\n---Lowercase example 2----')
 
name = 'PYTHON'
company = 'WELCOME TO TUTORIAL GATEWAY'
print(np.char.lower(name))
print(np.char.lower(company))
Numpy lower Function

Python numpy center (居中)

numpy center 用于填充字符串。 Numpy center 的语法

np.char.center(‘string’, length, ‘char’)

如果给定的长度小于原始长度,那么 Python 将删除原始字符串中的额外字符(截断)

如果长度大于原始长度,那么这些额外的空格将用给定的字符填充。顾名思义,原始字符串位于中心位置。

import numpy as np
 
print('---center---')
print(np.char.center('Hello', 11, '*'))
print(np.char.center('Welcome', 20, '#'))
print(np.char.center('Hello', 4, '*'))
 
print('\n--char center example----')
 
name = 'Python'
company = 'TutorialGateway'
print(np.char.center(name, 30, '@'))
print(np.char.center(company, 50, '$'))
print(np.char.center(company, 10, '$'))

center 函数输出

---center---
***Hello***
######Welcome#######
Hell

---char center example----
@@@@@@@@@@@@Python@@@@@@@@@@@@
$$$$$$$$$$$$$$$$$TutorialGateway$$$$$$$$$$$$$$$$$$
TutorialGa

Python Numpy strip (去除)

Numpy strip 会移除给定字符串左右两侧的空格。

import numpy as np
 
msg1 = '   Hello'
msg2 = 'Python        '
msg3 = '         Hello World           '
 
print('Original Message = ', msg1)
print('Strip Left       = ', np.char.strip(msg1))
 
print('\nOriginal Message  = ', msg2)
print('Strip Right         = ', np.char.strip(msg2))
 
print('\nOriginal Message   = ', msg3)
print('Strip Left & Right = ', np.char.strip(msg3))

strip 函数输出

Original Message =     Hello
Strip Left       =  Hello

Original Message  =  Python        
Strip Right         =  Python

Original Message   =           Hello World           
Strip Left & Right =  Hello World

Python numpy split (分割)

numpy split 函数根据指定的.分隔符分割给定的字符串

import numpy as np
 
print('---split---')
print(np.char.split('Hello World', sep = ' '))
print(np.char.split('hi,how are you?', sep = ' '))
print(np.char.split('hi,Python,Program', sep = ','))
 
print('\n----char split---')
 
msg = 'Welcome to Tutorial Gateway'
print(np.char.split(msg, sep = ' '))
 
msg2 = 'Welcome@Python@From@Tutorial@Gateway'
print(np.char.split(msg2, sep = '@'))

split 函数输出

---split---
['Hello', 'World']
['hi,how', 'are', 'you?']
['hi', 'Python', 'Program']

----char split---
['Welcome', 'to', 'Tutorial', 'Gateway']
['Welcome', 'Python', 'From', 'Tutorial', 'Gateway']

Python numpy splitlines (按行分割)

此 numpy splitlines 返回给定字符串中行的列表。numpy splitlines 考虑换行符 \n 和 \r。

import numpy as np
 
msg = 'Hello\nWorld'
print(msg)
print('\n-- splitlines output----')
print(np.char.splitlines(msg))
print()
 
msg1 = 'Hi\rHow are you?'
print(msg1)
print('\n-- splitlines output----')
print(np.char.splitlines(msg1))
print()
 
msg2 = 'Hi\rHow are \nyou?'
print(msg2)
print('\n-- splitlines output----')
print(np.char.splitlines(msg2))
 
print('\n--- splitlines---')
print(np.char.splitlines('Hello\nWorld'))

splitlines 函数输出

Hello
World

-- splitlines output----
['Hello', 'World']

Hi
How are you?

-- splitlines output----
['Hi', 'How are you?']

Hi
How are 
you?

-- splitlines output----
['Hi', 'How are ', 'you?']

---splitlines---
['Hello', 'World']

Python numpy join (连接)

numpy join 函数在给定字符串的每个字符之后连接指定的字符(第一个参数)。

import numpy as np
 
print('--- join---')
print(np.char.join(':', 'HMS'))
print(np.char.join('/', 'dmy'))
print(np.char.join('-', 'dmy'))
print(np.char.join(' ', 'HelloWorld'))
 
print('\n----char join---')
print(np.char.join(['-', ':'], ['dmy', 'mdy']))
print(np.char.join(['@', '$', ':'], ['Hi', 'Hello', 'HMS']))

join 函数输出

---join---
H:M:S
d/m/y
d-m-y
H e l l o W o r l d

----char join---
['d-m-y' 'm:d:y']
['H@i' 'H$e$l$l$o' 'H:M:S']

Python numpy replace (替换)

Python numpy replace 函数用于将子字符串替换为新字符串。

np.char.replace(original_string, old_string, new_string)

Numpy replace 函数在原始字符串中搜索旧字符串。如果找到,numpy replace 函数会用 new_string 替换。

import numpy as np
 
print(np.char.replace('Hello', 'l', 'K'))
print(np.char.replace('tutorial', 't', 'M'))
 
print(np.char.replace('oh boy! welcome to you', 'o', 'X'))
print(np.char.replace('oh boy! welcome to you', 'o', 'SSh'))

replace 函数输出

HeKKo
MuMorial
Xh bXy! welcXme tX yXu
SShh bSShy! welcSShme tSSh ySShu

这是 numpy 字符串 replace 函数的另一个示例。

import numpy as np
 
msg1 = 'Hello World'
msg2 = 'xyz working in xyz position at xyz company'
 
print('Original Message   = ', msg1)
print('Repalce l with T   = ', np.char.replace(msg1, 'l', 'T'))
print('Repalce o with HMS = ', np.char.replace(msg1, 'o', 'HMS'))
 
print('\nOriginal Message   = ', msg2)
print('Repalce o with T     = ', np.char.replace(msg2, 'o', 'T'))
print('Repalce xyz with abc = ', np.char.replace(msg2, 'xyz', 'abc'))

replace 函数输出

Original Message   =  Hello World
Repalce l with T   =  HeTTo WorTd
Repalce o with HMS =  HellHMS WHMSrld

Original Message   =  xyz working in xyz position at xyz company
Repalce o with T     =  xyz wTrking in xyz pTsitiTn at xyz cTmpany
Repalce xyz with abc =  abc working in abc position at abc company

Python numpy encode (编码)

numpy encode 用于使用指定的编码器对字符串进行编码。这里,np.char.encode(‘Hello’, ‘cp500’)使用 cp500 对 Hello 字符串进行编码

import numpy as np
 
print('---String encode---')
print(np.char.encode('Hello', 'cp500'))
print(np.char.encode('Hello', 'utf_16'))
print(np.char.encode('How are You?', 'cp500'))
print(np.char.encode('Welcome again!', 'utf_16'))
 
print('\n---char encode----')
name = 'Python'
company = 'Tutorial Gateway'
print(np.char.encode(name, 'cp500'))
print(np.char.encode(name, 'utf_16'))
print(np.char.encode(name, 'utf_16_le'))
 
print(np.char.encode(company, 'cp500'))
print(np.char.encode(company, 'utf_16'))
print(np.char.encode(company, 'utf_16_be'))

encode 函数输出

---String encode---
b'\xc8\x85\x93\x93\x96'
b'\xff\xfeH\x00e\x00l\x00l\x00o'
b'\xc8\x96\xa6@\x81\x99\x85@\xe8\x96\xa4o'
b'\xff\xfeW\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00a\x00g\x00a\x00i\x00n\x00!'

---char encode----
b'\xd7\xa8\xa3\x88\x96\x95'
b'\xff\xfeP\x00y\x00t\x00h\x00o\x00n'
b'P\x00y\x00t\x00h\x00o\x00n'
b'\xe3\xa4\xa3\x96\x99\x89\x81\x93@\xc7\x81\xa3\x85\xa6\x81\xa8'
b'\xff\xfeT\x00u\x00t\x00o\x00r\x00i\x00a\x00l\x00 \x00G\x00a\x00t\x00e\x00w\x00a\x00y'
b'\x00T\x00u\x00t\x00o\x00r\x00i\x00a\x00l\x00 \x00G\x00a\x00t\x00e\x00w\x00a\x00y'

Python numpy string decode (解码)

numpy decode 用于解码已编码(numpy encode)的字符串。这里,您必须指定用于解码消息的代码。如果您使用错误的代码,decode 函数将引发错误。

import numpy as np
 
print('---String decode---')
print(np.char.decode(b'\xc8\x85\x93\x93\x96', 'cp500'))
 
print('\n---char decode----')
name = b'\xd7\xa8\xa3\x88\x96\x95'
company = b'\xe3\xa4\xa3\x96\x99\x89\x81\x93@\xc7\x81\xa3\x85\xa6\x81\xa8'
print(np.char.decode(name, 'cp500'))
print(np.char.decode(company, 'cp500'))
 
print('\n---char decode----')
name = 'Python Numpy'
ecode = np.char.encode(name, 'cp500')
dcode = np.char.decode(ecode, 'cp500')

print('Original Message = ', name)
print('Encoded Message  = ', ecode)
print('Decoded Message  = ', dcode)

decode 输出

---String decode---
Hello

---char decode----
Python
Tutorial Gateway

---char decode----
Original Message =  Python Numpy
Encoded Message  =  b'\xd7\xa8\xa3\x88\x96\x95@\xd5\xa4\x94\x97\xa8'
Decoded Message  =  Python Numpy