Explore key concepts of string manipulation, including indexing, slicing, and extracting substrings. This quiz is designed to enhance your understanding of how to access, modify, and analyze strings effectively using common operations.
Given the string message = 'Adventure', what character does message[3] return?
Explanation: String indexing in most programming languages starts from zero, so message[3] refers to the fourth character. In 'Adventure', the fourth character is 'e'. The option 'd' represents the second character, 'v' is the third, and 'n' is the fifth, making them incorrect.
If text = 'Unicorn', what is the result of text[-2]?
Explanation: Negative indexing counts from the end of the string; text[-1] is the last character, so text[-2] is 'r'. The option 'o' is text[-3], 'n' is the last character, and 'c' is in the middle, so those are not the correct answer.
Given s = 'Notebook', what is produced by s[1:5]?
Explanation: The slicing s[1:5] starts at index 1 and goes up to, but does not include, index 5. That produces 'oteb'. 'Note' and 'Noteb' start at the wrong index, while 'otebo' includes one extra character beyond the slice.
Given 'Rainbow', which slice extracts 'bow'?
Explanation: In 'Rainbow', 'b' is at index 4, so word[4:7] captures 'b', 'o', 'w'. 'word[5:8]' goes past the end, 'word[3:6]' gives 'nbo', and 'word[6:9]' exceeds the string's length, returning only 'w' in some languages or causing an error.
For phrase = 'happy coding', which option checks if 'code' is a substring of phrase?
Explanation: The correct way to check for a substring is using the 'in' keyword: 'code' in phrase. The other options, like has(), check(), or finds(), are not standard string methods and would result in errors or undefined behavior.