0
0
Pandasdata~10 mins

str.len() for string length in Pandas - Interactive Code Practice

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

Complete the code to get the length of each string in the 'Name' column.

Pandas
df['Name_length'] = df['Name'].str.[1]()
Drag options to blanks, or click blank then click option'
Alen
Blength
Ccount
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' instead of 'len' causes an AttributeError.
Using 'count' counts occurrences of a substring, not string length.
2fill in blank
medium

Complete the code to filter rows where the 'City' name length is greater than 5.

Pandas
filtered = df[df['City'].str.len() [1] 5]
Drag options to blanks, or click blank then click option'
A<
B==
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' selects shorter or equal length strings, not longer ones.
Using '==' selects only strings exactly length 5.
3fill in blank
hard

Fix the error in the code to correctly calculate string lengths in the 'Country' column.

Pandas
lengths = df['Country'].str.[1]()
Drag options to blanks, or click blank then click option'
Alen
Blen()
Clength()
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' without parentheses returns the method object instead of the lengths.
Using 'length' or 'length()' causes AttributeError.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 4 characters.

Pandas
lengths = {word: word[1] for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
A__len__()
Blen()
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len()' in the first blank causes an error because 'len' is a function, not a method of string.
Using '<=' in the second blank selects shorter words, not longer.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values for words shorter than 6 characters.

Pandas
result = { [1]: [2] for word in words if len(word) [3] 6}
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C<
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.lower()' instead of 'word.upper()' for keys.
Using '>' instead of '<' in the filter condition.
Using 'word' instead of 'len(word)' for values.