Unit 2: String Manipulation, Lists & Tuple



String Manipulation in Python

A string is a sequence of characters enclosed in single quotes (' '), double quotes (" "), or triple quotes (''' ''').

Example: name = "Python"

Accessing Strings

Indexing Concept

Each character in a string has a position number (index).

Index Diagram

String : P y t h o n Index : 0 1 2 3 4 5 Negative:-6 -5 -4 -3 -2 -1

Accessing Characters

s = "Python" print(s[0]) # P print(s[-1]) # n

⚠ Strings are immutable (cannot be changed).

Basic Operations on Strings

a) Concatenation (+)

Joining two strings.

a = "Hello" b = "World" print(a + " " + b)

b) Repetition (*)

Repeats string multiple times.

print("Hi " * 3)

c) Length of String

s = "Python" print(len(s))

String Slicing

Slicing means extracting a part of a string.

Syntax: string[start : stop : step]

Slicing Diagram

String : P y t h o n Index : 0 1 2 3 4 5

Examples

s = "Python" print(s[0:4]) # Pyth print(s[2:]) # thon print(s[:3]) # Pyt print(s[::2]) # Pto print(s[::-1]) # nohtyP

String Functions

Functions are built-in utilities that operate on strings.

FunctionDescriptionExample
len()Length of stringlen("Hi")
max()Highest charactermax("abc")
min()Lowest charactermin("abc")
sorted()Sort characterssorted("python")

String Methods

Methods are functions applied on string objects.

Case Conversion Methods

MethodUse
upper()Convert to uppercase
lower()Convert to lowercase
title()First letter capital
capitalize()Capitalize first character
s = "python" print(s.upper())

Searching & Checking Methods

MethodPurpose
find()Find substring
index()Find position
startswith()Check start
endswith()Check end
s = "Python" print(s.find("t"))

Boolean Methods

MethodChecks
isalpha()Only alphabets
isdigit()Only digits
isalnum()Alphanumeric
isspace()Only spaces

Replace & Split Methods

MethodDescription
replace()Replace text
split()Convert to list
join()Join strings
s = "Hello World" print(s.replace("World", "Python")) print(s.split())

Important String Properties

PropertyDescription
ImmutableCannot modify
IndexedSupports indexing
IterableCan loop

Exam-Oriented Short Notes

  • Strings are immutable
  • Indexing starts from 0
  • Negative indexing supported
  • Slicing uses colon operator
  • Methods return new string

Difference: Function vs Method

FunctionMethod
StandaloneCalled on object
len(s)s.upper()

Important Exam Questions

  • Explain string slicing with example
  • What is immutability of string?
  • Difference between find() and index()
  • Explain positive & negative indexing

Lists in Python

A list is a collection of multiple values stored in a single variable.

Key Characteristics of List

  1. Ordered (elements have index)
  2. Mutable (can be changed)
  3. Allows duplicate values
  4. Can store different data types

Example: my_list = [10, 20, 30, "Python", 5.5]

Accessing List Elements

Indexing Concept

List : [10, 20, 30, 40, 50] Index : 0 1 2 3 4 Neg : -5 -4 -3 -2 -1

Examples

lst = [10, 20, 30, 40, 50] print(lst[0]) # 10 print(lst[-1]) # 50

List Slicing

Extracting part of a list.

Syntax

list[start : stop : step]

Examples

lst = [10, 20, 30, 40, 50] print(lst[1:4]) # [20, 30, 40] print(lst[:3]) # [10, 20, 30] print(lst[::2]) # [10, 30, 50]

Operations on Lists

a) Concatenation (+)

a = [1, 2] b = [3, 4] print(a + b)

b) Repetition (*)

print([1, 2] * 3)

c) Membership (in / not in)

print(2 in [1, 2, 3])

d) Length

print(len([1, 2, 3]))

Working with Lists

Modifying List Elements

lst = [10, 20, 30] lst[1] = 25 print(lst)

Adding Elements

lst.append(40) lst.insert(1, 15)

Removing Elements

lst.remove(25) lst.pop()

List Functions

FunctionDescriptionExample
len()Number of elementslen(lst)
max()Largest elementmax(lst)
min()Smallest elementmin(lst)
sum()Sum of elementssum(lst)
sorted()Sort listsorted(lst)

List Methods

Adding Methods

MethodUse
append()Add at end
insert()Add at index
extend()Add multiple

Removing Methods

MethodUse
remove()Remove specific
pop()Remove by index
clear()Remove all

Other Important Methods

MethodDescription
index()Position of element
count()Number of occurrences
sort()Sort list
reverse()Reverse list
copy()Copy list

List Immutability vs Mutability

ListString
MutableImmutable
Can modifyCannot modify

Looping through List

lst = [10, 20, 30] for i in lst: print(i)

Exam-Oriented Important Points

  • List is mutable
  • Index starts from 0
  • Supports slicing
  • Can store mixed data types
  • Methods change original list

Important Exam Questions

  • Explain list with features
  • Difference between append() and extend()
  • What is slicing in list?
  • List vs Tuple

Tuples in Python

A tuple is a collection of values stored in a single variable, similar to a list, but immutable.

Key Characteristics of Tuple

  • Ordered collection
  • Immutable (cannot be changed)
  • Allows duplicate elements
  • Can store different data types
  • Faster than list

Example: t = (10, 20, 30, "Python", 5.5)

Accessing Tuple Elements

Indexing Concept

Tuple : (10, 20, 30, 40, 50) Index : 0 1 2 3 4 Neg : -5 -4 -3 -2 -1

Examples

t = (10, 20, 30, 40, 50) print(t[0]) # 10 print(t[-1]) # 50

Tuple Slicing

Syntax

tuple[start : stop : step]

Examples

t = (10, 20, 30, 40, 50) print(t[1:4]) # (20, 30, 40) print(t[:3]) # (10, 20, 30) print(t[::2]) # (10, 30, 50)

Operations on Tuples

a) Concatenation (+)

t1 = (1, 2) t2 = (3, 4) print(t1 + t2)

b) Repetition (*)

print((1, 2) * 3)

c) Membership (in / not in)

print(2 in (1, 2, 3))

d) Length

print(len((1, 2, 3)))

Working with Tuples

Immutability Example

t = (10, 20, 30) t[1] = 25 # Error

Workaround (Convert to List)

t = (10, 20, 30) lst = list(t) lst[1] = 25 t = tuple(lst)

Tuple Packing

t = 10, 20, 30

Tuple Unpacking

a, b, c = t

Tuple Functions

FunctionDescriptionExample
len()Number of elementslen(t)
max()Largest elementmax(t)
min()Smallest elementmin(t)
sum()Sum of numeric elementssum(t)
sorted()Sort tuplesorted(t)

Tuple Methods

Tuple has only two built-in methods.

MethodUse
count()Count occurrences
index()Find position

Example

t = (10, 20, 20, 30) print(t.count(20)) print(t.index(30))

Tuple vs List (Exam Favorite)

FeatureTupleList
MutabilityImmutableMutable
SpeedFasterSlower
Syntax()[]
MethodsFewMany

Looping Through Tuple

t = (10, 20, 30) for i in t: print(i)

Exam-Oriented Important Points

  • Tuples are immutable
  • Indexing starts from 0
  • Supports slicing
  • Faster than list
  • Only count() and index() methods

Important Exam Questions

  • Define tuple with example
  • Difference between tuple and list
  • What is tuple unpacking?
  • Explain immutability of tuple