0
0
NumPydata~20 mins

String type in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy String Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this NumPy string array creation?
Consider the following code that creates a NumPy array of strings. What will be the output?
NumPy
import numpy as np
arr = np.array(['cat', 'dog', 'bird'], dtype='S4')
print(arr)
A[b'cat' b'dog' b'bird']
Bb'cat' b'dog' b'bird'
C]'drib'b 'god'b 'tac'b[
D[b'cat' b'dog' b'bir']
Attempts:
2 left
💡 Hint
Remember that dtype='S4' means each string is stored with 4 bytes, truncating longer strings.
data_output
intermediate
2:00remaining
What is the length of each element in this NumPy string array?
Given this NumPy array of strings, what is the length of each element stored internally?
NumPy
import numpy as np
arr = np.array(['apple', 'banana', 'cherry'], dtype='S6')
lengths = np.char.str_len(arr)
print(lengths)
A[5 6 6]
B[5 6 6 ]
C[5 6 6 7]
D]6 6 5[
Attempts:
2 left
💡 Hint
Check how dtype='S6' affects string storage and length calculation.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error when assigning a longer string?
Look at this code snippet. Why does it raise an error or truncate the string when assigning a longer string to a NumPy string array element?
NumPy
import numpy as np
arr = np.array(['cat', 'dog'], dtype='S3')
arr[0] = 'elephant'
print(arr)
AIt raises a ValueError because 'elephant' is longer than 3 bytes allowed by dtype='S3'.
BIt converts 'elephant' to bytes and stores fully without truncation.
CIt raises a TypeError because strings cannot be assigned to NumPy arrays.
DIt truncates 'elephant' to 'ele' silently without error.
Attempts:
2 left
💡 Hint
Consider how fixed-length string dtypes handle longer strings assigned to elements.
visualization
advanced
2:00remaining
Visualize the difference between Unicode and byte string arrays in NumPy
Which option shows the correct plot comparing the memory size of Unicode (U) and byte (S) string arrays of length 5 with 1000 elements?
NumPy
import numpy as np
import matplotlib.pyplot as plt
arr_s = np.array(['abcde']*1000, dtype='S5')
arr_u = np.array(['abcde']*1000, dtype='U5')
sizes = [arr_s.nbytes, arr_u.nbytes]
labels = ['S5', 'U5']
plt.bar(labels, sizes)
plt.ylabel('Memory size in bytes')
plt.title('Memory size: byte vs Unicode strings')
plt.show()
ABar chart with 'S5' bar larger than 'U5' bar
BPie chart showing 'S5' and 'U5' equal parts
CBar chart with 'S5' bar smaller than 'U5' bar
DLine chart showing sizes equal
Attempts:
2 left
💡 Hint
Unicode strings use more bytes per character than byte strings.
🧠 Conceptual
expert
2:00remaining
Which statement about NumPy string dtypes is TRUE?
Select the correct statement about fixed-length string dtypes in NumPy.
ANumPy Unicode string dtype 'U' stores each character as 1 byte, same as byte strings 'S'.
BNumPy byte string dtype 'S' stores strings as fixed-length bytes and truncates longer strings silently.
CAssigning a longer string than the dtype length to a NumPy string array element raises a runtime error.
DNumPy string arrays automatically resize to fit longer strings assigned to elements.
Attempts:
2 left
💡 Hint
Think about how fixed-length string arrays handle storage and assignment.