Class XI · Chapter 8Unit 2, Python Programming (45 marks)5 min read
Share:WhatsAppLinkedIn

Chapter 8: Strings

CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~6-8 marks Priority: CRITICAL, string operations and methods frequently asked


Key Concepts

8.1 Creating Strings

str1 = 'Hello'          # Single quotes
str2 = "Hello"           # Double quotes
str3 = '''Hello           # Triple quotes (multi-line)
World'''
str4 = """Hello World"""  # Triple double quotes
  • Strings are sequences of UNICODE characters
  • Strings are immutable (cannot change individual characters)

8.2 Accessing Characters (Indexing)

s = "HELLO"
# Positive index: 0  1  2  3  4
# Negative index: -5 -4 -3 -2 -1
s[0]    # 'H'
s[-1]   # 'O'
s[4]    # 'O'

8.3 String Slicing

s = "COMPUTER"
s[0:4]      # 'COMP'    (index 0 to 3)
s[2:6]      # 'MPUT'
s[:4]       # 'COMP'    (start defaults to 0)
s[4:]       # 'UTER'    (end defaults to len)
s[:]        # 'COMPUTER' (full string)
s[::2]      # 'CMUE'    (every 2nd character)
s[::-1]     # 'RETUPMOC' (reverse string)
s[-4:]      # 'UTER'

Syntax: string[start:stop:step], stop is excluded

8.4 String Operations

Concatenation (+)

"Hello" + " " + "World"    # "Hello World"

Repetition (*)

"Ha" * 3                   # "HaHaHa"

Membership (in, not in)

"llo" in "Hello"           # True
"xyz" not in "Hello"       # True

Comparison, Compared character by character using ASCII/Unicode values

  • "abc" < "abd" is True (compares at first different character)

8.5 Traversing a String

s = "Hello"
for ch in s:
    print(ch, end=" ")     # H e l l o

for i in range(len(s)):
    print(s[i], end=" ")   # H e l l o

8.6 Built-in Functions for Strings

Function Description Example
len(s) Length of string len("Hello") = 5
max(s) Character with highest ASCII max("Hello") = 'o'
min(s) Character with lowest ASCII min("Hello") = 'H'

8.7 String Methods

Method Description Example Output
upper() All uppercase "hello".upper() "HELLO"
lower() All lowercase "HELLO".lower() "hello"
title() Title case "hello world".title() "Hello World"
capitalize() First char uppercase "hello".capitalize() "Hello"
swapcase() Swap case "Hello".swapcase() "hELLO"
count(sub) Count occurrences "hello".count("l") 2
find(sub) Index of first occurrence (-1 if not found) "hello".find("ll") 2
index(sub) Like find() but raises ValueError if not found "hello".index("ll") 2
startswith(s) Starts with given string "Hello".startswith("He") True
endswith(s) Ends with given string "Hello".endswith("lo") True
replace(old, new) Replace substring "Hello".replace("l", "L") "HeLLo"
strip() Remove leading/trailing whitespace " hi ".strip() "hi"
lstrip() Remove leading whitespace " hi ".lstrip() "hi "
rstrip() Remove trailing whitespace " hi ".rstrip() " hi"
split(sep) Split into list "a,b,c".split(",") ['a','b','c']
join(list) Join list into string ",".join(['a','b']) "a,b"
isalpha() All alphabets? "Hello".isalpha() True
isdigit() All digits? "123".isdigit() True
isalnum() All alphanumeric? "abc123".isalnum() True
isspace() All whitespace? " ".isspace() True
isupper() All uppercase? "HELLO".isupper() True
islower() All lowercase? "hello".islower() True

Programs/Code Examples

Count Vowels in a String

s = input("Enter string: ")
count = 0
for ch in s:
    if ch in "aeiouAEIOU":
        count += 1
print("Vowels:", count)

Reverse a String

s = input("Enter string: ")
print("Reversed:", s[::-1])

Check Palindrome

s = input("Enter string: ")
if s == s[::-1]:
    print("Palindrome")
else:
    print("Not Palindrome")

Common Board Exam Question Patterns

  1. Find output of string slicing operations (2-3 marks)
  2. String methods output (2-3 marks)
  3. Write a program to count characters/vowels/words (3-4 marks)
  4. Difference between find() and index(), strip() variants (2 marks)
  5. String traversal using for loop (2 marks)
  6. Immutability question: Why can't we do s[0] = 'h'? (1-2 marks)

Key Points Students Miss

  1. Strings are immutable: s[0] = 'h' raises TypeError
  2. Slicing s[a:b] excludes index b (goes up to b-1)
  3. s[::-1] reverses the string, most elegant way
  4. find() returns -1 if not found; index() raises ValueError
  5. All string methods return a new string (original unchanged because immutable)
  6. split() without argument splits on whitespace (spaces, tabs, newlines)
  7. Negative index: -1 is the last character, -2 is second-to-last
  8. len() is a function, not a method (use len(s) not s.len())
  9. String concatenation with + requires BOTH operands to be strings
  10. "Hello" * 0 returns empty string ""

Test Your Knowledge

Take a quick quiz on this chapter

Start Quiz →

Prefer watching over reading?

Subscribe for free.

Subscribe on YouTube