Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

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']))
Python Numpy add Function 1

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]))
Python Numpy add Function 2

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']))
Python Numpy add Function 3

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

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'))
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))
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'))
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))
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(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))
Python Numpy lower Function

Python numpy center

The Python numpy center is for padding a string. The syntax of the Numpy center

np.char.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(np.char.center('Hello', 11, '*'))
print(np.char.center('Welcome', 20, '#'))
print(np.char.center('Hello', 4, '*'))
 
print('\n---Numpy 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, '$'))
Python Numpy center Function

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))
Python Numpy strip Function

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 = '@'))
Python Numpy split Function

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(msg)
print('-- Numpy splitlines output----')
print(np.char.splitlines(msg))
print()
 
msg1 = 'Hi\rHow are you?'
print(msg1)
print('-- Numpy splitlines output----')
print(np.char.splitlines(msg1))
print()
 
msg2 = 'Hi\rHow are \nyou?'
print(msg2)
print('-- Numpy splitlines output----')
print(np.char.splitlines(msg2))
 
print('\n---Numpy String splitlines---')
print(np.char.splitlines('Hello\nWorld'))
Python Numpy splitlines Method

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']))
Python Numpy join Function

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'))
Python Numpy replace Function 1

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'))
Python Numpy replace Function 2

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'))
Python Numpy encode Function

Python numpy 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)
Python Numpy decode Method

Filed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy