Class 12 Python – String Functions
Python provides a wide range of built-in string functions and methods. Here is a detailed
explanation of important string functions for Class 12 students.
1. **Basic String Functions** - len(string): Returns the length of the string. - max(string): Returns the
character with maximum ASCII value. - min(string): Returns the character with minimum ASCII
value. - sorted(string): Returns a sorted list of characters.
2. **Case Conversion Functions** - upper(): Converts all characters to uppercase. - lower():
Converts all characters to lowercase. - title(): Converts the first character of each word to
uppercase. - capitalize(): Converts the first character of string to uppercase. - swapcase(): Converts
lowercase to uppercase and vice versa.
3. **Searching and Finding** - find(substring): Returns the index of first occurrence (or -1). -
rfind(substring): Returns the index of last occurrence. - index(substring): Same as find but raises
ValueError if not found. - rindex(substring): Same as rfind but raises ValueError if not found. -
count(substring): Counts the occurrences of a substring.
4. **Validation Functions (Boolean Results)** - isalnum(): True if string contains letters and
numbers only. - isalpha(): True if string contains only letters. - isdigit(): True if string contains only
digits. - islower(): True if all characters are lowercase. - isupper(): True if all characters are
uppercase. - istitle(): True if string is in title case. - isspace(): True if string contains only spaces.
5. **Modification Functions** - strip(): Removes leading and trailing spaces. - lstrip(): Removes
spaces from left. - rstrip(): Removes spaces from right. - replace(old, new): Replaces old substring
with new. - join(iterable): Joins elements with given string as separator. - split(separator): Splits
string into list based on separator. - splitlines(): Splits string at line breaks.
6. **Formatting Functions** - format(): Used for string formatting. - center(width): Centers string
within given width. - ljust(width): Left-aligns string. - rjust(width): Right-aligns string. - zfill(width):
Pads string on left with zeros.
7. **Other Useful Functions** - startswith(substring): Checks if string starts with substring. -
endswith(substring): Checks if string ends with substring. - encode(): Encodes string to bytes. -
expandtabs(size): Replaces tabs with spaces.
**Examples:**
s = "Python Programming" print(s.upper()) → "PYTHON PROGRAMMING" print(s.find("Pro")) → 7
print(s.replace("Python", "Java")) → "Java Programming" print(s.isalpha()) → False
---
**Summary Table:**
Function | Purpose ----------------|----------------------------- len() | Length of string upper() | Convert to
uppercase lower() | Convert to lowercase find() | Find substring index replace() | Replace substring
split() | Split into list join() | Join list into string strip() | Remove spaces
These string functions help students manipulate text efficiently in Python, and are frequently asked
in board exams and practicals.