To explore the string methods in numpy
Sample string.
Output from above mentioned constructor of numpy.
ceneter()
capitalize()
lower()
upper()
split()
encode()
decode()
Import numpy library.
Declare the sample string.
Pass it to the appropriate constructor from numpy.
#import libraries
import numpy as np
#Sample string
sample_string = ‘hello python’
#center method
print(“Center method”)
print(np.char.center(sample_string, 20,fillchar = ‘*’))
print(“\n”)
#Capitalize method
print(“Capitalize method”)
print(np.char.capitalize(sample_string))
print(“\n”)
#title method
print(“title method”)
print(np.char.title(sample_string))
print(“\n”)
#lower method
print(“lower method”)
print(np.char.lower(sample_string))
print(“\n”)
#Upper method
print(“Upper method”)
print(np.char.upper(sample_string))
print(“\n”)
#split method
print(“split method”)
print(np.char.split (sample_string, sep = ‘,’))
print(“\n”)
#encode method
print(“encode method”)
e = np.char.encode(sample_string, ‘cp500′)
print(e,”\n”)
#decode metod
print(“decode method”)
print(np.char.decode(e,’cp500’))