Skip to content
Computer ScienceCBSE Class 11 · Strings

Python String Indexing, Slicing and Traversal Class 11: Outputs, Immutability and Palindromes

Updated 17 October 2026 · Fact-checked

Each character of a string has an index from 0 on the left and from -1 on the right. A slice s[start:stop:step] returns a new string from start up to but not including stop, moving by step; missing parts default to the ends, and a step of -1 reverses the string. Strings are immutable, so s[0] = "J" gives a TypeError; build a new string instead.

Every letter has two indices, and a slice s[2:5] stops before index 5, so the dashed T is left out and you get MPU.

Illustration drawn for QuestionBuddy.

Understand String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11)

Indexing. For s = "COMPUTER" the characters have indices C0 O1 M2 P3 U4 T5 E6 R7 and also C-8 O-7 M-6 P-5 U-4 T-3 E-2 R-1. len(s) is 8; the valid indices are -8 to 7. Any other index raises IndexError.

Slicing. s[start:stop:step] takes characters from start up to, but NOT including, stop.

  • Omitted start and stop mean the ends: s[:3] is "COM" and s[5:] is "TER".
  • Negative numbers count from the right: s[-3:] is "TER".
  • Slices never raise IndexError: s[3:100] is "PUTER" and s[10:] is "" (empty string).
  • A step skips characters: s[::2] is "CMUE". A negative step walks backwards: s[::-1] is "RETUPMOC" and s[5:2:-1] is "TUP" (indices 5, 4, 3).

Immutability. A string cannot be changed in place. s[0] = "J" raises TypeError. Make a new string and assign it: s = "J" + s[1:].

Operators. + joins strings only (not string and number), * repeats, in tests for a substring. * is done before +.

Traversal. for ch in s: visits each character; for i in range(len(s)): visits each index so s[i] can be used. A loop with range(len(s) - 1, -1, -1) visits the indices backwards.

Palindrome. A string that reads the same forwards and backwards: s == s[::-1]. The comparison is case-sensitive, so "Level" is not a palindrome as it stands.

w = "madam"
if w == w[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")   # prints Palindrome

String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11): key facts

Index ranges
  • positive 0 to len-1
  • negative -1 to -len
  • outside the range gives IndexError
For length 8 valid indices are -8 to 7.
Slice
  • s[start:stop:step]
  • stop is excluded
  • defaults are the two ends
  • slices never raise IndexError
s[:n] is the first n characters.
Common slices
  • s[::-1] reversed
  • s[::2] every second character
  • s[-n:] last n characters
A negative step reads from right to left.
Immutable
s[i] = x gives TypeError | s = s[:i] + x + s[i+1:] builds a new string
Methods return new strings.

How to answer String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11) questions

To evaluate a slice, list the indices it visits and read off the characters.

  1. 1Write the string with its indices above it, positive and negative.
  2. 2Fill in the defaults: start 0 and stop len for a positive step; start -1 and stop before the first character for a negative step.
  3. 3List the indices from start towards stop, excluding stop.
  4. 4Read the characters at those indices in order.
  5. 5For a loop trace, write the value of the loop variable and the string built so far at each pass.

String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11) in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

A student code

Suppose a school gives each student a code such as "SCI2025B07", where the first 3 characters name the stream, the next 4 the year and the last 3 the section and roll number.

Write slices for the stream, the year and the last 3 characters.

Try it first, then show the solutionSolution
  1. The stream is code[:3] giving "SCI".
  2. The year is code[3:7] giving "2025".
  3. The last 3 characters are code[-3:] giving "B07".

Answer: code[:3], code[3:7] and code[-3:].

Memory hook: Count the start index, and stop is one after the last wanted character.

Tricks to remember String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11)

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Stop is a fence

    The stop index is the first character you do NOT take.

    Use it when: Slices.

  • Minus one reverses

    A step of -1 starts at the end and walks to the front.

    Use it when: s[::-1] and palindromes.

  • Strings are sealed

    You can read any character but never replace one; make a new string instead.

    Use it when: TypeError questions.

String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11): solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Basic slice · 1 mark · MCQBoard level

For s = "COMPUTER", what is s[2:5]?
(a) "OMP"
(b) "MPU"
(c) "MPUT"
(d) "PUT"

Try it first, then show the solutionSolution
  1. Index 2 is M, 3 is P, 4 is U; stop 5 is excluded.

Answer: (b) "MPU"

Example 2: Several slices · 3 marks · Short answerBoard level

For s = "COMPUTER" give s[:3], s[-3:], s[::2], s[::-1] and s[10:]. (3 marks)

Try it first, then show the solutionSolution
  1. s[:3] takes indices 0, 1, 2: "COM".
  2. s[-3:] takes the last three: "TER".
  3. s[::2] takes indices 0, 2, 4, 6: "CMUE".
  4. s[::-1] reverses: "RETUPMOC".
  5. s[10:] starts beyond the end: "" (empty string), with no error.

Answer: "COM", "TER", "CMUE", "RETUPMOC", ""

Example 3: Palindrome test · 4 marks · Short answerHarder board level

Write a program that reads a word and prints whether it is a palindrome. Give the output for madam, level and Level. (4 marks)

Try it first, then show the solutionSolution
  1. Use: w = input("Word: "); if w == w[::-1]: print("Palindrome") else: print("Not a palindrome").
  2. madam reversed is madam: Palindrome.
  3. level reversed is level: Palindrome.
  4. Level reversed is leveL, which differs in case from Level: Not a palindrome, because comparison is case-sensitive.

Answer: madam: Palindrome; level: Palindrome; Level: Not a palindrome

Example 4: Loops and immutability · 5 marks · Short answerHOTS / competency

Trace the code and give the output of each print; then state what happens at the last line and how to fix it. (5 marks)

s = "Python"
r = ""
for i in range(len(s) - 1, -1, -2):
    r += s[i]
print(r)
t = "a1b2c3"
d = ""
for ch in t:
    if ch >= "0" and ch <= "9":
        d += ch
print(d)
s[0] = "J"
Try it first, then show the solutionSolution
  1. range(5, -1, -2) gives 5, 3, 1: s[5] = "n", s[3] = "h", s[1] = "y", so r = "nhy".
  2. In t, the characters from "0" to "9" are 1, 2, 3 in order, so d = "123".
  3. s[0] = "J" raises TypeError because a string does not support item assignment.
  4. Fix: s = "J" + s[1:], which gives "Jython".

Answer: nhy
123
TypeError at s[0] = "J"; use s = "J" + s[1:] to get "Jython".

Example 5: Prefix pattern · 4 marks · Short answerHarder board level

Give the output and the number of lines:

s = "ABCD"
for i in range(1, len(s) + 1):
    print(s[:i])
for i in range(len(s) - 1, 0, -1):
    print(s[:i])

(4 marks)

Try it first, then show the solutionSolution
  1. The first loop prints prefixes of length 1 to 4: A, AB, ABC, ABCD.
  2. The second loop uses i = 3, 2, 1 and prints ABC, AB, A.
  3. Total lines: 4 + 3 = 7.

Answer: A
AB
ABC
ABCD
ABC
AB
A (7 lines)

Where students lose marks in String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11)

The slips examiners see most often, and what to write instead.

  • Mistake: Including the stop character

    Counting the stop index as part of the slice.

    Wrong working

    s = "COMPUTER"; s[2:5] written as "MPUT"

    Right working

    s[2:5] = "MPU"

    Do this: Stop is excluded.

    Usually costs: 1 mark

  • Mistake: Trying to modify a character

    Treating a string like a list.

    Wrong working

    s[0] = "J"

    Right working

    s = "J" + s[1:]

    Do this: Create a new string.

    Usually costs: 1 to 2 marks

  • Mistake: Adding a number to a string

    Forgetting that + needs two strings.

    Wrong working

    "Roll" + 7

    Right working

    "Roll" + str(7)

    Do this: Convert with str().

    Usually costs: 1 mark

Exam tips

  • Write the indices under the string before slicing.
  • Remember that an out-of-range slice is an empty string, not an error.
  • Name the error type: TypeError for item assignment, IndexError for a single bad index.
  • For palindromes mention that the check is case-sensitive.

Practice questions on String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11)

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    For s = "HELLO", what is s[1:4]?
    (a) "HELL"
    (b) "ELL"
    (c) "ELLO"
    (d) "EL"

    Hint

    Indices 1, 2, 3.

    Answer

    (b) "ELL"

  2. Q2 · 1 mark · Assertion-reasonHarder board level

    Assertion (A): For s = "COMPUTER", the expression s[10:] gives an empty string and does not raise an error.
    Reason (R): Slicing stops silently at the boundaries of the string, while a single out-of-range index raises IndexError.
    (a) Both A and R are true, and R is the correct explanation of A.
    (b) Both A and R are true, but R is not the correct explanation of A.
    (c) A is true, but R is false.
    (d) A is false, but R is true.

    Hint

    Slice versus index.

    Answer

    (a) Both A and R are true, and R is the correct explanation of A.

  3. Q3 · 4 marks · Short answerHarder board level

    Give the output:

    s = "Computer Science"
    print(s[3:8], s[-7:], s[::3], len(s))

    (4 marks)

    Hint

    Count the space as index 8.

    Answer

    Indices: C0 o1 m2 p3 u4 t5 e6 r7 space8 S9 c10 i11 e12 n13 c14 e15, so len = 16. s[3:8] = "puter"; s[-7:] = indices 9 to 15 = "Science"; s[::3] takes indices 0, 3, 6, 9, 12, 15 = "CpeSee". Output: puter Science CpeSee 16

  4. Q4 · 3 marks · Short answerHarder board level

    Give the output:

    a = "ab"
    b = "cd"
    print(a + b * 2, (a + b) * 2, "b" in a + b, "bc" in a + b)

    (3 marks)

    Hint

    Repetition is done before concatenation.

    Answer

    b * 2 is "cdcd", so a + b * 2 is "abcdcd"; (a + b) * 2 is "abcdabcd"; "b" in "abcd" is True; "bc" in "abcd" is True. Output: abcdcd abcdabcd True True

  5. Q5 · 5 marks · Short answerHOTS / competency

    The following program has two errors. Identify them, correct the program and give its output.

    name = "sunita"
    name[0] = "S"
    print(name + 5)

    (5 marks)

    Hint

    One error is about changing, the other about joining.

    Answer

    Error 1: name[0] = "S" raises TypeError because strings are immutable. Error 2: name + 5 raises TypeError because a string cannot be joined to an int. Corrected: name = "S" + name[1:] and print(name + str(5)). Output: Sunita5

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What is the index of the last character?Show answer

    -1, or len(s) - 1.

  2. 2.Is the stop index included in a slice?Show answer

    No.

  3. 3.What does s[::-1] give?Show answer

    The reversed string.

  4. 4.What error does s[0] = "x" give?Show answer

    TypeError.

  5. 5.Does an out-of-range slice raise an error?Show answer

    No; it gives a shorter or empty string.

String Indexing, Slicing, Immutability and Traversal with Outputs (CBSE 11): frequently asked questions

Is there a character data type in Python?

No; a single character is a string of length 1.

Can I slice with a variable?

Yes; the start, stop and step can be any integer expressions.

Why is "Level" not a palindrome in code?

Because L and l are different characters, so the reverse leveL differs from Level.