0
0
NumPydata~10 mins

String type in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NumPy array of strings.

NumPy
import numpy as np
arr = np.array(['apple', 'banana', 'cherry'], dtype=[1])
print(arr)
Drag options to blanks, or click blank then click option'
Anp.str_
Bbool
Cfloat
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using dtype='int' or dtype='float' for string arrays.
Using dtype=str which gives object dtype array.
2fill in blank
medium

Complete the code to create a fixed-length string array of length 5.

NumPy
import numpy as np
arr = np.array(['cat', 'dog', 'bird'], dtype=[1])
print(arr)
print(arr.dtype)
Drag options to blanks, or click blank then click option'
A'S5'
B'int32'
C'U10'
D'float64'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'U10' which is for Unicode strings, not byte strings.
Using numeric dtypes for string arrays.
3fill in blank
hard

Fix the error in the code to create a Unicode string array.

NumPy
import numpy as np
arr = np.array(['red', 'green', 'blue'], dtype=[1])
print(arr)
print(arr.dtype)
Drag options to blanks, or click blank then click option'
A'int64'
B'S10'
C'U10'
D'bool_'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'S10' which creates byte strings, not Unicode.
Using numeric or boolean dtypes for strings.
4fill in blank
hard

Fill both blanks to create a NumPy array of strings and check its type.

NumPy
import numpy as np
arr = np.array(['one', 'two', 'three'], dtype=[1])
print(type(arr[0]))  # Should print <class '[2]'>
Drag options to blanks, or click blank then click option'
A'U5'
Bnumpy.str_
Cstr
D'int32'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's built-in str type instead of numpy.str_.
Using numeric dtype instead of string dtype.
5fill in blank
hard

Fill all three blanks to create a fixed-length byte string array and convert it to Unicode strings.

NumPy
import numpy as np
byte_arr = np.array(['a', 'bb', 'ccc'], dtype=[1])
unicode_arr = byte_arr.astype([2])
print(unicode_arr)
print(unicode_arr.dtype == [3])
Drag options to blanks, or click blank then click option'
A'S3'
Bstr
C'U3'
Dnp.str_
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's str instead of np.str_.
Confusing byte string dtype 'S3' with Unicode dtype 'U3'.