3 String Operations
title: "3_String_operations" date: 2026-05-24T13:51:32Z
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)
['Hello world' 'Say something']
Q2. Repeat x three time element-wise.
1x = 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.
1x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)
2capitalized = ...
3lowered = ...
4uppered = ...
5swapcased = ...
6titlecased = ...
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 = ...
3left = ...
4right = ...
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 = ...
3decoded = ...
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)
['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 = ...
3lstripped = ...
4rstripped = ...
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)
[['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)
[['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)
['0034']
Q11. Replace "John" with "Jim" in x.
1x = np.array(['Hello nmy name is John'], dtype=np.str)
['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)
[ 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)
[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)
[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)
[ 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 = ...
3out2 = ...
4out3 = ...
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)
[False True True True]