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
Accessing Characters
⚠ Strings are immutable (cannot be changed).
Basic Operations on Strings
a) Concatenation (+)
Joining two strings.
b) Repetition (*)
Repeats string multiple times.
c) Length of String
String Slicing
Slicing means extracting a part of a string.
Syntax: string[start : stop : step]
Slicing Diagram
Examples
String Functions
Functions are built-in utilities that operate on strings.
| Function | Description | Example |
|---|---|---|
| len() | Length of string | len("Hi") |
| max() | Highest character | max("abc") |
| min() | Lowest character | min("abc") |
| sorted() | Sort characters | sorted("python") |
String Methods
Methods are functions applied on string objects.
Case Conversion Methods
| Method | Use |
|---|---|
| upper() | Convert to uppercase |
| lower() | Convert to lowercase |
| title() | First letter capital |
| capitalize() | Capitalize first character |
Searching & Checking Methods
| Method | Purpose |
|---|---|
| find() | Find substring |
| index() | Find position |
| startswith() | Check start |
| endswith() | Check end |
Boolean Methods
| Method | Checks |
|---|---|
| isalpha() | Only alphabets |
| isdigit() | Only digits |
| isalnum() | Alphanumeric |
| isspace() | Only spaces |
Replace & Split Methods
| Method | Description |
|---|---|
| replace() | Replace text |
| split() | Convert to list |
| join() | Join strings |
Important String Properties
| Property | Description |
|---|---|
| Immutable | Cannot modify |
| Indexed | Supports indexing |
| Iterable | Can 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
| Function | Method |
|---|---|
| Standalone | Called on object |
len(s) | s.upper() |
Important Exam Questions
- Explain string slicing with example
- What is immutability of string?
- Difference between
find()andindex() - Explain positive & negative indexing
Lists in Python
A list is a collection of multiple values stored in a single variable.
Key Characteristics of List
- Ordered (elements have index)
- Mutable (can be changed)
- Allows duplicate values
- Can store different data types
Example: my_list = [10, 20, 30, "Python", 5.5]
Accessing List Elements
Indexing Concept
Examples
List Slicing
Extracting part of a list.
Syntax
Examples
Operations on Lists
a) Concatenation (+)
b) Repetition (*)
c) Membership (in / not in)
d) Length
Working with Lists
Modifying List Elements
Adding Elements
Removing Elements
List Functions
| Function | Description | Example |
|---|---|---|
| len() | Number of elements | len(lst) |
| max() | Largest element | max(lst) |
| min() | Smallest element | min(lst) |
| sum() | Sum of elements | sum(lst) |
| sorted() | Sort list | sorted(lst) |
List Methods
Adding Methods
| Method | Use |
|---|---|
| append() | Add at end |
| insert() | Add at index |
| extend() | Add multiple |
Removing Methods
| Method | Use |
|---|---|
| remove() | Remove specific |
| pop() | Remove by index |
| clear() | Remove all |
Other Important Methods
| Method | Description |
|---|---|
| index() | Position of element |
| count() | Number of occurrences |
| sort() | Sort list |
| reverse() | Reverse list |
| copy() | Copy list |
List Immutability vs Mutability
| List | String |
|---|---|
| Mutable | Immutable |
| Can modify | Cannot modify |
Looping through List
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
Examples
Tuple Slicing
Syntax
Examples
Operations on Tuples
a) Concatenation (+)
b) Repetition (*)
c) Membership (in / not in)
d) Length
Working with Tuples
Immutability Example
Workaround (Convert to List)
Tuple Packing
Tuple Unpacking
Tuple Functions
| Function | Description | Example |
|---|---|---|
| len() | Number of elements | len(t) |
| max() | Largest element | max(t) |
| min() | Smallest element | min(t) |
| sum() | Sum of numeric elements | sum(t) |
| sorted() | Sort tuple | sorted(t) |
Tuple Methods
Tuple has only two built-in methods.
| Method | Use |
|---|---|
| count() | Count occurrences |
| index() | Find position |
Example
Tuple vs List (Exam Favorite)
| Feature | Tuple | List |
|---|---|---|
| Mutability | Immutable | Mutable |
| Speed | Faster | Slower |
| Syntax | () | [] |
| Methods | Few | Many |
Looping Through Tuple
Exam-Oriented Important Points
- Tuples are immutable
- Indexing starts from 0
- Supports slicing
- Faster than list
- Only
count()andindex()methods
Important Exam Questions
- Define tuple with example
- Difference between tuple and list
- What is tuple unpacking?
- Explain immutability of tuple