NumPy String Operations

If you want to work on string data then NumPy string operations methods help to do work easy. The python NumPy support a bunch of string operations, string comparison, and string information methods.

So lets start with

Python NumPy String Operations Methods

To start the use of string methods need to import NumPy package and some raw string data.

1

2

3

import numpy as np # import numpy package

ch_name = "Indian AI Production" # create string

str1 = "Learning Python NumPy" # create string

np.char.add()

This function use to add two strings.

Syntax: np.char.add(string_array1, string_array2)

1

2

add_string = np.char.add(ch_name, str1) # add two strings ch_name and str1

print("Added two string ch_name and str1 = ", add_string)

1

Output >>> Added two string ch_name and str1 =  Indian AI ProductionLearning Python NumPy

Click here to jump on python NumPy tutorial.

np.char.multiply()

This function helps to duplicate string required number of times.

Syntax: np.char.multiply(string_array, number)

1

2

duplicate_ch_name = np.char.multiply(ch_name, 2) # multiply ch_name 2 times

print("Duplicated ch_name = ", duplicate_ch_name) # print duplicate_ch_name

1

Output >>> Duplicated ch_name =  Indian AI ProductionIndian AI Production

np.char.capitalize()

The capitalize() function convert given string’s first character in capital format.

Syntax: np.char.capitalize(string_array)

1

2

cap_ch_name = np.char.capitalize(ch_name) # capital ch_name

print("Capitalize ch_name = ", cap_ch_name) # print cap_ch_name

1

Output >>> Capitalize ch_name =  Indian ai production

np.char.lower()

The lower() function convert given string in lower case..

Syntax: np.char.lower(string_array)

1

2

low_ch_name = np.char.lower(ch_name) # lower ch_name

print("Lower ch_name = ", low_ch_name) # print ch_name

1

Output >>> Lower ch_name =  indian ai production

np.char.upper()

The upper() function convert given string in upper case..

Syntax: np.char.upper(string_array)

1

2

upp_ch_name = np.char.upper(ch_name) # upper ch_name

print("Upper ch_name = ", upp_ch_name) # print upp_ch_name

1

Output >>> Upper ch_name =  INDIAN AI PRODUCTION

np.char.title()

The title() function convert given string in title format..

Syntax: np.char.title(string_array)

1

2

tit_upp_ch_name = np.char.title(upp_ch_name) # title upp_ch_name

print("Title upp_ch_name = ", tit_upp_ch_name) # print tit_upp_ch_name

1

Output >>> Title upp_ch_name =  Indian Ai Production

np.char.center()

The title() function set the ng in the center of given string length. If you want to fill the ng’s side black space. Then give fillchar parameter with character.

Syntax: np.char.center(string_array, width, fillchar=‘ ‘)

1

2

cen_ch_name = np.char.center(ch_name, 30, fillchar = "*") # center upp_ch_name

print("Center ch_name = ", cen_ch_name) # print cen_ch_name

1

Output >>> Center cen_ch_name =  *****Indian AI Production*****

np.char.split()

The split() function split the string in a list of items. By default split function split the string by space.

Syntax: np.char.split(string_array, sep=None, maxsplit=None)

1

2

spl_ch_name = np.char.split(ch_name) # split ch_name

print("Split spl_ch_name = ", spl_ch_name) # print spl_ch_name

1

Output >>> Split ch_name =  ['Indian', 'AI', 'Production']

np.char.splitlines()

The splitlines() function split the string by lines in a list as elements.

Syntax: np.char.splitlines(string_array, keepends=None)

1

2

splitlines_str = np.char.splitlines("Indian\nAi\nProduction") # splitlines string

print("Splitlines  = ", splitlines_str ) # print splitlines_str

1

Output >>> Splitlines  =  ['Indian', 'Ai', 'Production']

np.char.join()

The join() function join the echa character of given sequence by seperator.

Syntax: np.char.join(string_array_seperator, string_array)

1

2

join_dmy = np.char.join([":", "/"], ["dmy", "dmy"])

print("Joint dmy by : and / = ", join_dmy)

1

Output >>> Joint dmy by : and / =  ['d:m:y' 'd/m/y']

np.char.replace()

The replace() function replace existing string by new given string.

Syntax: np.char.replace(string_array, old_string_array, new_string_array, count=None)

1

2

3

# Replace "AI" by "Artificial Intelligence" from ch_name

rep_ch_name = np.char.replace(ch_name, "AI", "Artificial Intelligence")

print("Replace old string by new = ", rep_ch_name)

1

Output >>> Replace old string by new =  Indian Artificial Intelligence Production

Python NumPy String Comparison 

The string comparison methods use to compare string with each other and return a boolean value.

np.char.equal()

The equal() function return “True” boolean value, If both strings are same else “False”.

Syntax: np.char.equal(string_array1,  string_array2)

or use equal to operator

string_array1 == string_array2

1

2

equ_str = np.char.equal(ch_name, str1) # check equality

print("Is ch_name equal to str1 = ", equ_str )

1

Output >>> Is ch_name equal to str1 =  False

np.char.not_equal()

The not_equal() function return “True” boolean value, If both strings are not same else “False”.

Syntax: np.char.not_equal(string_array1,  string_array2)

or use equal to operator

string_array1 !string_array2

1

2

not_equ_str = np.char.equal(ch_name, str1) # check  non equality

print("Is ch_name not equal to str1 = ", equ_str )

1

Output >>> Is ch_name not equal to str1 =  False

Python NumPy string Information

The string information methods use to get information from the stings.

np.char.count()

The count() function count string from an existing string and return number.

Syntax: np.char.count(string_array, sub, start=0, end=None)

1

2

count_A = np.char.count(ch_name, "A") # count string "A"

print("Character 'A' present ", count_A , " times in a string - ", ch_name)

1

Output >>> Character 'A' present  1  times in a string -  Indian AI Production

np.char.find()

The find() function find given string from an existing string and return the index of the first character of that string.

Syntax: np.char.find(string_array, sub, start=0, end=None)

1

2

find_AI = np.char.count(ch_name, "AI") # Find string "AI"

print("String  'AI' found at index ", find_AI , " from string - ", ch_name)

1

Output >>> String  'AI' found at index  1  from string -  Indian AI Production

np.char.index()

The index() function find an index of a given string from an existing string and return the index of the first character of that string. If string will not found then return ValueError.

Syntax: np.char.index(string_array, sub, start=0, end=None)

1

2

index_AI = np.char.index(ch_name, "m") # Find index "m"

print("Index of 'm' string found at - ", find_AI , " from string - ", ch_name)

Output >>> 

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-f70fbeb5e5eb> in <module>
----> 1 index_AI = np.char.index(ch_name, "m") # Find index "m"
      2 print("Index of 'm' string found at - ", find_AI , " from string - ", ch_name)

~\Anaconda3\lib\site-packages\numpy\core\defchararray.py in index(a, sub, start, end)
    680     """
    681     return _vec_string(
--> 682         a, integer, 'index', [sub, start] + _clean_args(end))
    683 
    684 def isalnum(a):

ValueError: substring not found

np.char.isalpha()

The isalpha() function return “True”. If the string contains each and every character are the alphabet.

Syntax: np.char.isalpha(string_array)

1

2

str_isalpha = np.char.isalpha("Hello")

print("Is String 'Hello' alphabet - ", str_isalpha )

1

Output >> Is String 'Hello' alphabet -  True

np.char.isdecimal()

The isdecimal() function return “True”. If the string contains each and every character are decimal.

Syntax: np.char.isdecimal(string_array)

1

2

str_isdecimal = np.char.isdecimal("Hello")

print("Is String 'Hello' decimal - ", str_isdecimal )

1

Output >>> Is String 'Hello' decimal -  False

np.char.isdigit()

The isdigit() function return “True”. If the string contains each and every character are digits.

Syntax: np.char.isdigit(string_array)

1

2

str_isdigit = np.char.isdigit("Hello")

print("Is String 'Hello' digit - ", str_isdigit )

1

Output >>> Is String 'Hello' digit -  False

np.char.islower()

The islower() function return “True”. If string contain each and every character in lowercase.

Syntax: np.char.islower(string_array)

1

2

str_islower = np.char.islower("Hello")

print("Is String 'Hello' lower - ", str_islower )

1

Output >>> Is String 'Hello' lower -  False

np.char.isupper()

The isupper() function return “True”. If string contain each and every character in uppercase.

Syntax: np.char.isupper(string_array)

1

2

str_isupper = np.char.isupper("Hello India")

print("Is String 'Hello India' uppercase - ", str_isupper )

1

Output >>> Is String 'Hello India' uppercase -  False

np.char.isnumeric()

The isnumeric() function return “True”. If string contain each and every character in numeric form.

Syntax: np.char.isnumeric(string_array)

1

2

str_isnumeric = np.char.isnumeric("Hello")

print("Is String 'Hello' numeric - ", str_isnumeric )

1

Output >>> Is String 'Hello' numeric -  False

np.char.isspace()

The isspace() function return “True”. If string contain black space.

Syntax: np.char.isspace(string_array)

1

2

str_isspace = np.char.isspace("Hello India")

print("Is String 'Hello India' space - ", str_isspace )

1

Output >>> Is String 'Hello India' space -  False

To learn more about Python NumPy in detail click below button.

 Python NumPy Tutorial

Click here to refer or learn more methods on official site of scipy.org.

Conclusion

The python NumPy string operations have a number of methods and remember that method is a bit difficult. So don’t worry this all methods newer ever use in your professional life. If you want to use it refer to this blog.