Complete the code to create a NumPy array of strings.
import numpy as np arr = np.array(['apple', 'banana', 'cherry'], dtype=[1]) print(arr)
Using dtype=np.str_ creates a NumPy array with string type elements.
Complete the code to create a fixed-length string array of length 5.
import numpy as np arr = np.array(['cat', 'dog', 'bird'], dtype=[1]) print(arr) print(arr.dtype)
Using dtype='S5' creates a fixed-length byte string array with length 5.
Fix the error in the code to create a Unicode string array.
import numpy as np arr = np.array(['red', 'green', 'blue'], dtype=[1]) print(arr) print(arr.dtype)
Unicode strings in NumPy use dtype starting with 'U', so 'U10' means Unicode strings of length 10.
Fill both blanks to create a NumPy array of strings and check its type.
import numpy as np arr = np.array(['one', 'two', 'three'], dtype=[1]) print(type(arr[0])) # Should print <class '[2]'>
Using dtype='U5' creates Unicode strings of length 5. The type of each element is numpy.str_.
Fill all three blanks to create a fixed-length byte string array and convert it to Unicode strings.
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])
We create a byte string array with dtype 'S3'. Then convert it to Unicode strings using astype(np.str_). The resulting dtype is 'U3'.