CBSE Class 12 Python - String Slicing Assignment
Given:
text = "WelcomeToCBSEClass12"
Use the above string `text` for all slicing-related questions below.
Instructions:
- All questions are compulsory.
- Write the output or the correct slice for each.
1. Extract the word 'Welcome' from the string.
Answer: text[0:7]
2. Extract the word 'CBSE' using positive indexing.
Answer: text[9:13]
3. Extract the word 'Class12' using negative indexing.
Answer: text[-8:]
4. Write a slice that returns the string 'ToCBSE'.
Answer: text[7:13]
5. Write the slice that extracts characters from index 7 to 10 (inclusive).
Answer: text[7:11]
6. Write a slice to extract every second character from the full string.
Answer: text[::2]
7. Reverse the entire string using slicing.
Answer: text[::-1]
8. Extract 'WelcomeTo' using slicing.
Answer: text[:9]
9. Write a slice to get all characters from index 11 till the end.
Answer: text[11:]
10. Write a slice to get all characters from the beginning to index 6 (inclusive).
Answer: text[:7]
11. Extract the last 5 characters using negative indexing.
Answer: text[-5:]
12. Write a slice that skips one character each time starting from index 0.
Answer: text[::2]
13. Slice the string to exclude the first 3 and last 3 characters.
Answer: text[3:-3]
14. Extract the word 'ToCBSEClass' using slicing.
Answer: text[7:17]
15. Write a slicing statement that results in an empty string.
Answer: text[5:2]