Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 57 of 102
Marks: +1, -0
Consider the statements given below and then choose the correct output from the given options :
myStr="MISSISSIPPI"
print(myStr[:4]+"#"+myStr[-5:])
[CBSE 2024 Set 4]
Solution:  
Concept:
s[:n] gives the first n characters; s[-n:] gives the last n characters.
Solution:
myStr = "MISSISSIPPI" (11 characters, index 0 to 10)
myStr[:4] = index 0 to 3 = MISS
myStr[-5:] = last 5 characters = SIPPI
Joined with #: MISS#SIPPI
Key point: The stop index is not included, so [:4] gives 4 characters, not 5.
Answer:
Option B (MISS#SIPPI)
© examsnet.com
Advertisements
Go to Question: