Complete the code to get the length of the string.
word = "hello" length = len([1]) print(length)
len inside len().The len() function returns the number of characters in the string variable word.
Complete the code to get the first character of the string.
word = "world" first_char = word[[1]] print(first_char)
String indexing starts at 0, so word[0] gives the first character.
Fix the error in the code to get the last character of the string.
word = "python" last_char = word[[1]] print(last_char)
len(word) which is out of range.The last character is at index len(word) - 1 because indexing starts at 0.
Fill both blanks to create a substring from the second to the fourth character (inclusive).
word = "example" substring = word[[1]:[2]] print(substring)
Substring slicing uses start index inclusive and end index exclusive. To get characters 2 to 4, use word[1:4].
Fill all three blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.
words = ["cat", "house", "dog", "elephant"] lengths = [1]: [2] for word in words if len(word) [3] 3 print(lengths)
The dictionary comprehension syntax is {key: value for item in list if condition}. Here, keys are words and values are their lengths. The condition filters words longer than 3 characters.