📖 Scenario: You work in a small company that stores customer names in a list. You want to use NumPy to handle these names efficiently for future analysis.
🎯 Goal: You will create a NumPy array of customer names using the string data type, then filter names based on length, and finally display the filtered names.
📋 What You'll Learn
Create a NumPy array with string type
Use a variable to set the minimum length for filtering
Use a list comprehension to filter names by length
Print the filtered names
💡 Why This Matters
🌍 Real World
Handling customer or user names efficiently is common in data science for cleaning and preparing data.
💼 Career
Knowing how to work with string data in NumPy helps in data preprocessing tasks in many data science and analytics jobs.
Progress0 / 4 steps
1
Create a NumPy array of customer names
Import NumPy as np and create a NumPy array called names with these exact strings: 'Alice', 'Bob', 'Charlie', 'David', 'Eva'. Make sure the array uses the string data type 'U'.
NumPy
Hint
Use np.array() with dtype='U' to create a string array.
2
Set the minimum length for filtering
Create a variable called min_length and set it to 5. This will be the minimum number of characters a name must have to be selected.
NumPy
Hint
Just assign the number 5 to min_length.
3
Filter names by minimum length
Use a list comprehension to create a list called filtered_names that contains only the names from names whose length is greater than or equal to min_length. Use for name in names and len(name) to check the length.
NumPy
Hint
Use [name for name in names if len(name) >= min_length] to filter.
4
Print the filtered names
Print the variable filtered_names to display the list of names that meet the length condition.
NumPy
Hint
Use print(filtered_names) to show the result.
Practice
(1/5)
1. Which NumPy string type should you use to store Unicode text like emojis or accented characters?
easy
A. Use 'U' type for Unicode strings.
B. Use 'S' type for Unicode strings.
C. Use 'i' type for Unicode strings.
D. Use 'f' type for Unicode strings.
Solution
Step 1: Understand NumPy string types
NumPy has two main string types: 'S' for byte strings (ASCII) and 'U' for Unicode strings.
Step 2: Identify correct type for Unicode
Unicode strings require 'U' type to support characters beyond ASCII, like emojis or accented letters.
Final Answer:
Use 'U' type for Unicode strings. -> Option A
Quick Check:
Unicode text = 'U' type [OK]
Hint: Unicode needs 'U', ASCII uses 'S' type [OK]
Common Mistakes:
Using 'S' type for Unicode causes data loss
Confusing numeric types with string types
Assuming 'U' is for byte strings
2. Which of the following is the correct way to create a NumPy array of byte strings with length 5?
easy
A. np.array(['hello', 'world'], dtype='int5')
B. np.array(['hello', 'world'], dtype='U5')
C. np.array(['hello', 'world'], dtype='S5')
D. np.array(['hello', 'world'], dtype='float5')
Solution
Step 1: Identify byte string dtype syntax
Byte strings use dtype='S' followed by the fixed length, e.g., 'S5' for length 5.
Step 2: Check options for correct dtype
np.array(['hello', 'world'], dtype='S5') uses 'S5', which is correct for byte strings of length 5.
Final Answer:
np.array(['hello', 'world'], dtype='S5') -> Option C
Quick Check:
Byte strings = 'S' + length [OK]
Hint: Byte strings use dtype='S' plus length number [OK]
A. The array must be created with dtype='S3' to assign 'abc'.
B. You cannot assign strings to NumPy arrays.
C. dtype='U2' only supports byte strings, not Unicode.
D. The string 'abc' is longer than dtype='U2' allows.
Solution
Step 1: Understand dtype='U2' limitation
dtype='U2' means Unicode strings of fixed length 2 characters only.
Step 2: Check assigned string length
Trying to assign 'abc' (3 characters) to an element that only allows 2 characters causes truncation or error.
Final Answer:
The string 'abc' is longer than dtype='U2' allows. -> Option D
Quick Check:
Fixed-length Unicode strings truncate or error on longer input [OK]
Hint: Check string length vs dtype length before assignment [OK]
Common Mistakes:
Thinking dtype='U2' supports any length string
Believing NumPy arrays can't hold strings
Confusing byte strings ('S') with Unicode ('U')
5. You have a list of mixed-length Unicode strings: ['hi', 'hello', 'hey', 'hola']. You want to create a NumPy array that stores all strings without truncation. Which dtype should you choose?
hard
A. dtype='U5' because the longest string has 5 characters.
B. dtype='S5' because it supports Unicode strings up to length 5.
C. dtype='U4' because it is the default for Unicode strings.
D. dtype='S10' to allow extra space for all strings.
Solution
Step 1: Find the longest string length
The longest string is 'hello' with 5 characters.
Step 2: Choose dtype to fit longest string
Use dtype='U5' to store Unicode strings up to length 5 without truncation.
Step 3: Avoid byte string dtype for Unicode
Using 'S' type stores byte strings, not Unicode, so it may cause data loss.
Final Answer:
dtype='U5' because the longest string has 5 characters. -> Option A
Quick Check:
Unicode max length = dtype='U' + max length [OK]
Hint: Match dtype length to longest Unicode string [OK]
Common Mistakes:
Using 'S' dtype for Unicode strings
Choosing dtype length smaller than longest string
Assuming default dtype fits all strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.