Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Python Numpy String Functions

by suresh

The Python Numpy string functions are used to alter the given string as per your requirement. The Numpy string functions are add, multiply, capitalise, title, upper, lower, centre, split, splitlines, strip, join, replace, encode and decode. For instance, Numpy upper function converts string to uppercase.

The following list of examples helps you understand these Python Numpy string functions.

Python Numpy add

The python Numpy add function is 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']))

OUTPUT

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

OUTPUT

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

OUTPUT

Python Numpy add Function 3

Python Numpy multiply

The Python Numpy multiply function is used to multiply or repeat a given string for specified number of times. In this example, p.char.multiply(‘hello ‘, 2) repeats hello string for 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))

OUTPUT

Python Numpy multiply Function

Python Numpy capitalize

The Python Numpy capitalise function is used to capitalise 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))

OUTPUT

Python Numpy capitalize Method

Python Numpy title

The Python Numpy title function is used 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))

OUTPUT

Python Numpy title Function

Python Numpy upper

The Python Numpy upper function 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))

OUTPUT

Python Numpy upper Function

Python Numpy lower

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

OUTPUT

Python Numpy lower Function

Python Numpy center

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

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

If the given length is less than the original string length then it will remove the extra characters from the original string (trim)

If length is greater than the original string length then those extra spaces will be filled with the given character. As the name suggests, original string will be in 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, '$'))

OUTPUT

Python Numpy center Function

Python Numpy strip

The Python Numpy strip removes empty spaces from Both the right and left side 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))

OUTPUT

Python Numpy strip Function

Python Numpy split

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

OUTPUT

Python Numpy split Function

Python Numpy splitlines

This Python Numpy splitlines retunes a list of lines in a given string. This will consider 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'))

OUTPUT

Python Numpy splitlines Method

Python Numpy join

The Python Numpy join function joins the specified character(first Argument) after each any every 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']))

OUTPUT

Python Numpy join Function

Python Numpy replace

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

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

The Numpy replace function search for old string in a original string. If it finds, Numpy replace function will 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'))

OUTPUT

Python Numpy replace Function 1

This is an another example of 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'))

OUTPUT

Python Numpy replace Function 2

Python Numpy encode

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

OUTPUT

Python Numpy encode Function

Python Numpy decode

The Python Numpy decode is used to decode the already encoded (Numpy encode) string. Here, you have to specify the code that have to use for decoding a message. If you use wrong code then it will throw 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)

OUTPUT

Python Numpy decode Method

Placed Under: Python

Trending Posts

Python CopySign

Add Reference Lines in Tableau

Fuzzy Grouping Transformation in SSIS

SQL @@SPID

Python atan

Python Program to Calculate Square of a Number

Java Program to Swap Two Arrays without temp

Find all Tables that Contain Specific Column Name

SQL Concatenate Rows into String

INSTEAD of UPDATE Triggers in SQL Server

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy