Python numpy String Functions

The Python numpy string functions are to alter the given string as per your requirement. The numpy string functions are: add, multiply, capitalize, title, upper, lower, center, split, splitlines, strip, join, replace, encode, and decode. For instance, the numpy string upper function converts a string to uppercase. These examples helps you understand the Python numpy string functions.

Python numpy add

The python numpy add function used for string concatenation. In this example, we are using this numpy add function to add two strings

import numpy as np

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

numpy add function to perform string concatenation output

Numpy String concatenation

This time we declared two different strings. Next, we used this numpy char add function 

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

numpy char add function output

Numpy String concatenation
['Suresh Tutorial Gateway']

Here, we are concatenating a string variable and a string

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

numpy char add output

Numpy String concatenation
['Suresh Working at Tutorial Gateway']

Python numpy multiply

The Python numpy multiply function used to multiply or repeat a given string for a specified number of times. In this example, p.char.multiply(‘hello ‘, 2) repeats hello string twice.

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

Python numpy capitalize

The Python numpy capitalize function used to capitalize or uppercase the first letter in a complete string.

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

Python numpy title

The Python numpy title function is to convert the first letter of each word to uppercase

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

Python numpy upper

The Python numpy upper converts all the letters in a string to uppercase 

import numpy as np

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

Python numpy lower

This Python numpy lower converts all the characters in a string to lowercase

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

Python numpy center

The Python numpy center is for padding a string. The syntax of the Numpy center‘string’, length, ‘char’)

If the given length is less than the original string length, then Python removes the extra characters from the original string (trim)

If length is greater than the original string length, then those extra spaces filled with the given character. As the name suggests, the original string in the center position.

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

numpy center function output

---Numpy String center---

---Numpy char center example----
@@@@@@@@@@@@[email protected]@@@@@@@@@@@

Python Numpy strip

The Python Numpy strip removes empty spaces from Both the right and left sides of a given string.

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('\Original Message   = ', msg3)
print('Strip Left & Right = ', np.char.strip(msg3))

numpy strip string function output

Original Message =     Hello
Strip Left       =  Hello

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

Python numpy split

The Python numpy split function splits the given string based on the specified separator

import numpy as np
print('---Numpy String 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----Numpy char split---')
msg = 'Welcome to Tutorial Gateway'
print(np.char.split(msg, sep = ' '))
msg2 = '[email protected]@[email protected]@Gateway'
print(np.char.split(msg2, sep = '@'))

numpy string split function output

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

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

Python numpy splitlines

This Python numpy splitlines returns a list of lines in a given string. The numpy splitlines considers the line breaks \n and \r.

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

numpy String splitlines function output

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

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

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

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

Python numpy join

The Python numpy join function joins the specified character(first Argument) after each character in a given string.

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

numpy join function output

---Numpy String join---
H e l l o W o r l d

----Numpy char join---
['d-m-y' 'm:d:y']
['[email protected]' 'H$e$l$l$o' 'H:M:S']

Python numpy replace

The Python numpy replace function is used to replace the substring with a new string

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

The Numpy replace function searches for an old string in an original string. If it finds, the numpy replace function replace with 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'))

numpy replace function output

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

It is another example of a numpy string replace function.

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'))

numpy string replace function output

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

The Python numpy encode used to encode the string using the specified encoder. Here, np.char.encode(‘Hello’, ‘cp500’) encode Hello string using cp500

import numpy as np
print('---Numpy 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---Numpy 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'))

numpy string encode function output

---Numpy String encode---
b'\xc8\x96\[email protected]\x81\x99\[email protected]\xe8\x96\xa4o'
b'\xff\xfeW\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00a\x00g\x00a\x00i\x00n\x00!'

---Numpy char encode----
b'\xe3\xa4\xa3\x96\x99\x89\x81\[email protected]\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

The Python numpy decode used to decode the already encoded (numpy encode) string. Here, you have to specify the code that has to use for decoding a message. If you use the wrong code, then numpy decode function throws an error.

import numpy as np
print('---Numpy String decode---')
print(np.char.decode(b'\xc8\x85\x93\x93\x96', 'cp500'))
print('\n---Numpy char decode----')
name = b'\xd7\xa8\xa3\x88\x96\x95'
company = b'\xe3\xa4\xa3\x96\x99\x89\x81\[email protected]\xc7\x81\xa3\x85\xa6\x81\xa8'
print(np.char.decode(name, 'cp500'))
print(np.char.decode(company, 'cp500'))
print('\n---Numpy 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)

Numpy string decode output

---Numpy String decode---

---Numpy char decode----
Tutorial Gateway

---Numpy char decode----
Original Message =  Python Numpy
Encoded Message  =  b'\xd7\xa8\xa3\x88\x96\[email protected]\xd5\xa4\x94\x97\xa8'
Decoded Message  =  Python Numpy