The default string type in NumPy arrays is fixed-length ASCII strings, represented as S followed by the length, for example S10 means a string of length 10.
Click to reveal answer
beginner
How do you create a NumPy array of Unicode strings?
You create a NumPy array of Unicode strings by specifying the data type as U followed by the maximum length, for example U5 for Unicode strings up to length 5.
Click to reveal answer
intermediate
What happens if you assign a longer string to a NumPy string array with fixed length?
If you assign a string longer than the fixed length, NumPy will truncate the string to fit the defined length, potentially losing data.
Click to reveal answer
intermediate
How can you convert a NumPy array of bytes strings to Unicode strings?
You can convert by using the astype method with a Unicode dtype, for example arr.astype('U') converts byte strings to Unicode strings.
Click to reveal answer
beginner
Why might you prefer Unicode strings (U) over byte strings (S) in NumPy?
Unicode strings support a wide range of characters from many languages, while byte strings only support ASCII or raw bytes. Use Unicode for international text.
Click to reveal answer
What does the dtype S8 mean in a NumPy array?
AA string of variable length
BA Unicode string of length 8
CAn 8-character fixed-length ASCII string
DAn 8-bit integer
✗ Incorrect
S8 means a fixed-length ASCII string of length 8 characters.
How do you specify a Unicode string type of length 10 in NumPy?
A<code>S10</code>
B<code>U10</code>
C<code>unicode10</code>
D<code>str10</code>
✗ Incorrect
U10 specifies a Unicode string of length 10 in NumPy.
What happens if you assign a string longer than the dtype length in a NumPy string array?
AThe string is truncated to fit the length
BAn error is raised
CThe string is stored fully ignoring length
DThe array resizes automatically
✗ Incorrect
NumPy truncates the string to the fixed length defined by the dtype.
Which method converts a byte string array to Unicode strings in NumPy?
A<code>astype('U')</code>
B<code>to_unicode()</code>
C<code>decode()</code>
D<code>convert_unicode()</code>
✗ Incorrect
The astype('U') method converts byte strings to Unicode strings.
Why choose Unicode strings (U) over byte strings (S) in NumPy?
AByte strings support emojis
BByte strings are faster
CUnicode strings use less memory
DUnicode supports international characters
✗ Incorrect
Unicode strings support many languages and characters beyond ASCII.
Explain how NumPy handles string data types and the difference between S and U types.
Think about ASCII vs Unicode and fixed length.
You got /5 concepts.
Describe how to convert a NumPy array of byte strings to Unicode strings and why this might be useful.
Consider encoding and international characters.
You got /4 concepts.
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.