Python Strings - Detailed Notes for Grade 11
1. What is a String?
A string is a sequence of characters enclosed in single ' ', double " ", or triple quotes ''' ''' / .
2. String Indexing
Each character has a position (index) starting from 0.
Example:
text = "Python"
text[0] -> 'P'
text[-1] -> 'n'
3. String Slicing
Extract parts of a string (substring).
text = "Programming"
text[0:6] -> 'Progra'
text[3:] -> 'gramming'
text[:5] -> 'Progr'
4. String Concatenation
Joining two or more strings.
first = "Hello"
second = "World"
first + " " + second -> 'Hello World'
5. Escape Characters
\n : New line
\t : Tab space
\\ : Backslash
\' : Single quote
\" : Double quote
6. String Formatting
name = "Ali"
age = 17
f"My name is {name} and I am {age} years old."
7. Common String Methods
- len(): Returns number of characters.
len("Python") -> 6
- lower(): Converts to lowercase.
- upper(): Converts to uppercase.
- capitalize(): Capitalizes the first letter.
- title(): Capitalizes each word.
- strip(): Removes spaces from both ends.
- lstrip()/rstrip(): Remove spaces from left/right.
- find(): Returns first index of substring.
- index(): Same as find(), but errors if not found.
- replace(): Replaces a substring.
- count(): Counts occurrences of substring.
- split(): Splits into a list.
- join(): Joins list into a string.
- startswith()/endswith(): Checks beginning/end.
- isalpha(): True if only letters.
- isdigit(): True if only digits.
- isalnum(): True if letters and digits.
- isspace(): True if only whitespace.
8. Summary Table
Method | Description
------------------ | --------------------------------
len() | Length of string
lower() | Convert to lowercase
upper() | Convert to uppercase
capitalize() | Capitalize first letter
title() | Capitalize each word
strip() | Remove whitespace
lstrip()/rstrip() | Remove left/right whitespace
find()/index() | Find substring
replace() | Replace substring
count() | Count occurrences
split() | Split into list
join() | Join list into string
startswith()/endswith() | Check beginning/end
isalpha() | Check for only letters
isdigit() | Check for only digits
isalnum() | Check for alphanumeric
isspace() | Check for only spaces