Skip to content
Question

Q.Consider the statements given below and then choose the correct output from the given options: myStr="MISSISSIPPI" print(myStr[:4]+"#"+myStr[-5:]) (A) MISSI#SIPPI (B) MISS#SIPPI (C) MISS#IPPIS (D) MISSI#IPPIS

CBSECBSE Class XII Board 2024MCQ· 1mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

String slicing in Python extracts portions of a string using index notation; myStr[:4] takes the first four characters, and myStr[-5:] takes the last five, which are then concatenated with # in between.

When you work with strings in Python, slicing is one of the most elegant operations at your disposal. It allows you to carve out any substring you need by specifying start and end positions. The syntax [start:end] extracts characters from index start up to (but not including) index end. When you omit the start, Python assumes you mean "from the beginning," and when you omit the end, it means "to the very end."

Let's walk through the given code with the string "MISSISSIPPI", which has 11 characters total (indices 0 through 10).

The first slice is myStr[:4]. Because there's no starting index, Python begins at position 0 and goes up to (but not including) index 4. That gives us:

  • Index 0: M
  • Index 1: I
  • Index 2: S
  • Index 3: S

So myStr[:4] produces "MISS".

The second slice is myStr[-5:]. Negative indices count backward from the end of the string. In "MISSISSIPPI", index -5 refers to the fifth character from the end. Let's map it out:

  • Index -5: S (the 7th character overall, at position 6)
  • Index -4: I
  • Index -3: P
  • Index -2: P
  • Index -1: I …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.