Skip to content

Commit dc03a52

Browse files
committed
Numpy String Functions
1 parent 91c78c0 commit dc03a52

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Numpy/P06_NumpyStringFunctions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Author: OMKAR PATHAK
2+
3+
import numpy as np
4+
5+
abc = ['abc']
6+
xyz = ['xyz']
7+
8+
# string concatenation
9+
print(np.char.add(abc, xyz)) # ['abcxyz']
10+
11+
print(np.char.add(abc, 'pqr')) # ['abcpqr']
12+
13+
# string multiplication
14+
print(np.char.multiply(abc, 3)) # ['abcabcabc']
15+
16+
# numpy.char.center: This function returns an array of the required width so that the input string is
17+
# centered and padded on the left and right with fillchar.
18+
19+
print(np.char.center(abc, 20, fillchar = '*')) # ['********abc*********']
20+
21+
# numpy.char.capitalize(): This function returns the copy of the string with the first letter capitalized.
22+
print(np.char.capitalize('hello world')) # Hello world
23+
24+
# numpy.char.title(): This function returns a title cased version of the input string with the first letter
25+
# of each word capitalized.
26+
print(np.char.title('hello how are you?')) # Hello How Are You?
27+
28+
# numpy.char.lower(): This function returns an array with elements converted to lowercase. It calls
29+
# str.lower for each element.
30+
print(np.char.lower(['HELLO','WORLD'])) # ['hello' 'world']
31+
32+
# numpy.char.upper(): This function calls str.upper function on each element in an array to return
33+
# the uppercase array elements.
34+
print(np.char.upper('hello')) # HELLO
35+
36+
# numpy.char.split(): This function returns a list of words in the input string. By default, a whitespace
37+
# is used as a separator
38+
print(np.char.split('Omkar Pathak')) # ['Omkar', 'Pathak']
39+
print(np.char.split('2017-02-11', sep='-')) # ['2017', '02', '11']
40+
41+
# numpy.char.join(): This method returns a string in which the individual characters are joined by
42+
# separator character specified.
43+
print(np.char.join(':','dmy')) # d:m:y

0 commit comments

Comments
 (0)