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 |
|
np.char.add()
This function use to add two strings.
Syntax: np.char.add(string_array1, string_array2)
1 2 |
|
1 |
|
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 |
|
1 |
|
np.char.capitalize()
The capitalize() function convert given string’s first character in capital format.
Syntax: np.char.capitalize(string_array)
1 2 |
|
1 |
|
np.char.lower()
The lower() function convert given string in lower case..
Syntax: np.char.lower(string_array)
1 2 |
|
1 |
|
np.char.upper()
The upper() function convert given string in upper case..
Syntax: np.char.upper(string_array)
1 2 |
|
1 |
|
np.char.title()
The title() function convert given string in title format..
Syntax: np.char.title(string_array)
1 2 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
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 |
|
1 |
|
np.char.isspace()
The isspace() function return “True”. If string contain black space.
Syntax: np.char.isspace(string_array)
1 2 |
|
1 |
|
To learn more about Python NumPy in detail click below button.
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.
Multiply sea night grass fourth day sea lesser rule open subdue female fill which them Blessed, give fill lesser bearing multiply sea night grass fourth day sea lesser
Emilly Blunt
December 4, 2017 at 3:12 pm