3 String Operations Solutions


title: "compare" date: 2026-05-24T13:51:35Z

String operations

1from __future__ import print_function
2import numpy as np
1author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"
1np.__version__
'1.11.3'

Q1. Concatenate x1 and x2.

1x1 = np.array(['Hello', 'Say'], dtype=np.str)
2x2 = np.array([' world', ' something'], dtype=np.str)
3out = np.char.add(x1, x2)
4print(out)
['Hello world' 'Say something']

Q2. Repeat x three time element-wise.

1x = np.array(['Hello ', 'Say '], dtype=np.str)
2out = np.char.multiply(x, 3)
3print(out)
['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.

 1x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)
 2capitalized = np.char.capitalize(x)
 3lowered = np.char.lower(x)
 4uppered = np.char.upper(x)
 5swapcased = np.char.swapcase(x)
 6titlecased = np.char.title(x)
 7print("capitalized =", capitalized)
 8print("lowered =", lowered)
 9print("uppered =", uppered)
10print("swapcased =", swapcased)
11print("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 _.

1x = np.array(['hello world', 'say something'], dtype=np.str)
2centered = np.char.center(x, 20, fillchar='_')
3left = np.char.ljust(x, 20, fillchar='_')
4right = np.char.rjust(x, 20, fillchar='_')
5
6print("centered =", centered)
7print("left =", left)
8print("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.

1x = np.array(['hello world', 'say something'], dtype=np.str)
2encoded = np.char.encode(x, 'cp500')
3decoded = np.char.decode(encoded,'cp500')
4print("encoded =", encoded)
5print("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.

1x = np.array(['hello world', 'say something'], dtype=np.str)
2out = np.char.join(" ", x)
3print(out)
['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.

1x = np.array(['   hello world   ', '\tsay something\n'], dtype=np.str)
2stripped = np.char.strip(x)
3lstripped = np.char.lstrip(x)
4rstripped = np.char.rstrip(x)
5print("stripped =", stripped)
6print("lstripped =", lstripped)
7print("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.

1x = np.array(['Hello my name is John'], dtype=np.str)
2out = np.char.split(x)
3print(out)
[['Hello', 'my', 'name', 'is', 'John']]

Q9. Split the element of x to multiple lines.

1x = np.array(['Hello\nmy name is John'], dtype=np.str)
2out = np.char.splitlines(x)
3print(out)
[['Hello', 'my name is John']]

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

1x = np.array(['34'], dtype=np.str)
2out = np.char.zfill(x, 4)
3print(out)
['0034']

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

1x = np.array(['Hello nmy name is John'], dtype=np.str)
2out = np.char.replace(x, "John", "Jim")
3print(out)
['Hello nmy name is Jim']

Comparison

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

1x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)
2x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)
3out = np.char.equal(x1, x2)
4print(out)
[ True  True  True  True False]

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

1x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)
2x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)
3out = np.char.not_equal(x1, x2)
4print(out)
[False False False False  True]

String information

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

1x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)
2out = np.char.count(x, "l")
3print(out)
[2 0 0 0 1]

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

1x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)
2out = np.char.find(x, "l")
3print(out)
4
5# compare
6# print(np.char.index(x, "l"))
7# => This raises an error!
[ 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.

1x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=np.str)
2out1 = np.char.isdigit(x)
3out2 = np.char.islower(x)
4out3 = np.char.isupper(x)
5print("Digits only =", out1)
6print("Lower cases only =", out2)
7print("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".

1x = np.array(['he', 'his', 'him', 'his'], dtype=np.str)
2out = np.char.startswith(x, "hi")
3print(out)
[False  True  True  True]