String operations


In [1]:
from __future__ import print_function
import numpy as np

In [2]:
author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"

In [3]:
np.__version__


Out[3]:
'1.11.3'

Q1. Concatenate x1 and x2.


In [4]:
x1 = np.array(['Hello', 'Say'], dtype=np.str)
x2 = np.array([' world', ' something'], dtype=np.str)


['Hello world' 'Say something']

Q2. Repeat x three time element-wise.


In [5]:
x = np.array(['Hello ', 'Say '], dtype=np.str)


['Hello Hello Hello ' 'Say Say Say ']

Q3-1. Capitalize the first letter of x element-wise.
Q3-2. Lowercase x element-wise.
Q3-3. Uppercase x element-wise.
Q3-4. Swapcase x element-wise.
Q3-5. Title-case x element-wise.


In [6]:
x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)
capitalized = ...
lowered = ...
uppered = ...
swapcased = ...
titlecased = ...
print("capitalized =", capitalized)
print("lowered =", lowered)
print("uppered =", uppered)
print("swapcased =", swapcased)
print("titlecased =", titlecased)


capitalized = ['Hello world' 'Say something']
lowered = ['hello world' 'say something']
uppered = ['HELLO WORLD' 'SAY SOMETHING']
swapcased = ['HEllO WOrlD' 'sAY SoMETHING']
titlecased = ['Hello World' 'Say Something']

Q4. Make the length of each element 20 and the string centered / left-justified / right-justified with paddings of _.


In [7]:
x = np.array(['hello world', 'say something'], dtype=np.str)
centered = ...
left = ...
right = ...

print("centered =", centered)
print("left =", left)
print("right =", right)


centered = ['____hello world_____' '___say something____']
left = ['hello world_________' 'say something_______']
right = ['_________hello world' '_______say something']

Q5. Encode x in cp500 and decode it again.


In [8]:
x = np.array(['hello world', 'say something'], dtype=np.str)
encoded = ...
decoded = ...
print("encoded =", encoded)
print("decoded =", decoded)


encoded = [b'\x88\x85\x93\x93\x96@\xa6\x96\x99\x93\x84'
 b'\xa2\x81\xa8@\xa2\x96\x94\x85\xa3\x88\x89\x95\x87']
decoded = ['hello world' 'say something']

Q6. Insert a space between characters of x.


In [9]:
x = np.array(['hello world', 'say something'], dtype=np.str)


['h e l l o   w o r l d' 's a y   s o m e t h i n g']

Q7-1. Remove the leading and trailing whitespaces of x element-wise.
Q7-2. Remove the leading whitespaces of x element-wise.
Q7-3. Remove the trailing whitespaces of x element-wise.


In [10]:
x = np.array(['   hello world   ', '\tsay something\n'], dtype=np.str)
stripped = ...
lstripped = ...
rstripped = ...
print("stripped =", stripped)
print("lstripped =", lstripped)
print("rstripped =", rstripped)


stripped = ['hello world' 'say something']
lstripped = ['hello world   ' 'say something\n']
rstripped = ['   hello world' '\tsay something']

Q8. Split the element of x with spaces.


In [11]:
x = np.array(['Hello my name is John'], dtype=np.str)


[['Hello', 'my', 'name', 'is', 'John']]

Q9. Split the element of x to multiple lines.


In [12]:
x = np.array(['Hello\nmy name is John'], dtype=np.str)


[['Hello', 'my name is John']]

Q10. Make x a numeric string of 4 digits with zeros on its left.


In [13]:
x = np.array(['34'], dtype=np.str)


['0034']

Q11. Replace "John" with "Jim" in x.


In [14]:
x = np.array(['Hello nmy name is John'], dtype=np.str)


['Hello nmy name is Jim']

Comparison

Q12. Return x1 == x2, element-wise.


In [15]:
x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)
x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)


[ True  True  True  True False]

Q13. Return x1 != x2, element-wise.


In [16]:
x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)
x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)


[False False False False  True]

String information

Q14. Count the number of "l" in x, element-wise.


In [17]:
x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)


[2 0 0 0 1]

Q15. Count the lowest index of "l" in x, element-wise.


In [18]:
x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)


[ 2 -1 -1 -1  2]

Q16-1. Check if each element of x is composed of digits only.
Q16-2. Check if each element of x is composed of lower case letters only.
Q16-3. Check if each element of x is composed of upper case letters only.


In [19]:
x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=np.str)
out1 = ...
out2 = ...
out3 = ...
print("Digits only =", out1)
print("Lower cases only =", out2)
print("Upper cases only =", out3)


Digits only = [False False False  True False False]
Lower cases only = [False False  True False  True  True]
Upper cases only = [False  True False False False False]

Q17. Check if each element of x starts with "hi".


In [20]:
x = np.array(['he', 'his', 'him', 'his'], dtype=np.str)


[False  True  True  True]

In [ ]: