CS7 Worksheet2 Strings
CS7 Worksheet2 Strings
Worksheet #2
Python Programming
Strings (Slicing, Negative Indexing, Advanced Indexing)
Instructions:
Answer the following questions by writing the output of the given Python code snippets or filling
in the blanks.
Answer: ________________
2. What is the output of this code when slicing from the end?
Answer: ________________
Answer: ________________
4. Fill in the blank to get the output "thon" from the string:
text = "Python"
print(text[___:___])
Answer: print(text[_____:_____])
text = "Programming"
print(text[0:11:2])
Answer: ________________
text = "ABCDEFGHIJ"
print(text[2:8])
Answer: ________________
text = "Experience"
print(text[___:___])
Answer: print(text[_____:_____])
Answer: ________________
10. What will be the output of this code using step slicing?
text = "123456789"
print(text[::3])
Answer: ________________
Answer: ________________
12. Fill in the blank to extract "Sci" from "Science" using slicing:
text = "Science"
print(text[___:___])
Answer: print(text[_____:_____])
Answer: ________________
14. What will be the output of the following code when slicing with a negative index?
Answer: ________________
15. What will be the output when slicing the last three characters?
Answer: ________________
16. What is the output when slicing every second character from index 1 to 9?
text = "Information"
print(text[1:9:2])
Answer: ________________
text = "abcdefghijkl"
print(text[::4])
Answer: ________________
Answer: ________________
19. What is the output when slicing every third character starting from index 2?
text = "1234567890"
print(text[2::3])
Answer: ________________
20. Write a Python statement that extracts and prints "gram" from the string text =
"Programming" using slicing.
text = "Programming"
print(_________________)
Answer: print(__________)