0
0
NumPydata~5 mins

String type in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the default string type in NumPy arrays?
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
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>
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
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>
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
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.