0
0
NumPydata~10 mins

String type in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String type in NumPy
Create NumPy array with strings
Array stores fixed-length strings
Access or modify elements
Observe dtype and behavior
We create a NumPy array of strings, which stores fixed-length strings internally. We can access or change elements and observe how NumPy handles string data.
Execution Sample
NumPy
import numpy as np
arr = np.array(['cat', 'dog', 'bird'])
print(arr)
print(arr.dtype)
arr[1] = 'fish'
print(arr)
Create a NumPy array of strings, print it and its type, change one element, then print the array again.
Execution Table
StepActionArray ContentdtypeNotes
1Create array with ['cat', 'dog', 'bird']['cat' 'dog' 'bird']dtype('<U4')Strings stored as fixed-length Unicode of length 4
2Print array['cat' 'dog' 'bird']dtype('<U4')Output shows all strings
3Print dtypeN/Adtype('<U4')dtype shows Unicode strings of max length 4
4Assign 'fish' to arr[1]['cat' 'fish' 'bird']dtype('<U4')'fish' fits max length 4, replaces 'dog'
5Print array after change['cat' 'fish' 'bird']dtype('<U4')Array updated with new string
6Assign 'elephant' to arr[0]['elep' 'fish' 'bird']dtype('<U4')String truncated to 4 chars due to fixed length
7Print array after truncation['elep' 'fish' 'bird']dtype('<U4')Long string cut to fit dtype length
💡 Array stores fixed-length Unicode strings; longer strings get truncated to dtype max length.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 6Final
arrundefined['cat' 'dog' 'bird']['cat' 'fish' 'bird']['elep' 'fish' 'bird']['elep' 'fish' 'bird']
Key Moments - 2 Insights
Why does assigning 'elephant' to arr[0] result in 'elep' instead of the full word?
NumPy arrays with string dtype store fixed-length strings. The dtype '<U4' means max 4 characters. Longer strings get cut off to fit, as shown in execution_table step 6.
What does dtype '<U4' mean in the context of the string array?
It means Unicode strings with a fixed max length of 4 characters. This is why all strings in the array have length up to 4, as seen in step 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the content of the array after assigning 'fish' to arr[1]?
A['cat' 'dog' 'bird']
B['cat' 'fish' 'bird']
C['cat' 'fish' 'fish']
D['cat' 'dog' 'fish']
💡 Hint
Check the 'Array Content' column at step 4 in the execution_table.
At which step does the array first show truncated string values?
AStep 4
BStep 2
CStep 6
DStep 1
💡 Hint
Look for when 'elephant' is assigned and truncated in the execution_table.
If the dtype was '<U10' instead of '<U4', what would happen when assigning 'elephant' to arr[0]?
AIt would store the full 'elephant' string
BIt would be truncated to 4 characters
CIt would cause an error
DIt would store only the first character
💡 Hint
Consider dtype max length and how it affects string storage shown in variable_tracker.
Concept Snapshot
NumPy string arrays store fixed-length Unicode strings.
The dtype '<U4' means max 4 characters per string.
Assigning longer strings truncates them to dtype length.
Access and modify elements like normal arrays.
Useful for consistent string length storage in arrays.
Full Transcript
This visual execution shows how NumPy handles string arrays. We start by creating an array of strings ['cat', 'dog', 'bird']. NumPy stores these as fixed-length Unicode strings with dtype '<U4', meaning max 4 characters. When we print the array and its dtype, we see the strings and the type. Changing an element to 'fish' works fine because 'fish' fits in 4 characters. But assigning 'elephant' to an element truncates it to 'elep' because of the fixed length. This behavior is important to understand when working with string data in NumPy arrays.