Python String and List Built-in Functions Reference
Python String Built-in Functions
Case Conversion Functions
upper()
👉 Definition: Converts all characters in a string to uppercase. It returns a new string without modifying the original.
👉 Example: "hello".upper() # "HELLO"
lower()
👉 Definition: Converts all characters in a string to lowercase.
👉 Example: "HELLO".lower() # "hello"
title()
👉 Definition: Capitalizes the first letter of every word and converts the remaining letters to lowercase.
👉 Example: "hello world".title() # "Hello World"
capitalize()
👉 Definition: Capitalizes only the first character of the string and converts the rest to lowercase.
👉 Example: "hello WORLD".capitalize() # "Hello world"
swapcase()
👉 Definition: Converts uppercase letters to lowercase and vice versa.
👉 Example: "AbC".swapcase() # "aBc"
Searching Functions
find(sub)
👉 Definition: Returns the index of the first occurrence of the substring. Returns -1 if not found.
👉 Example: "hello".find("l") # 2
index(sub)
👉 Definition: Returns the index of the substring; raises a ValueError if the substring is not found.
rfind(sub)
👉 Definition: Searches for the substring from the right side and returns the index of the last occurrence.
count(sub)
👉 Definition: Returns the number of times a substring appears in the string.
👉 Example: "hello".count("l") # 2
Boolean Checking Functions
isalnum(): Checks if the string contains only alphanumeric characters.isalpha(): Checks if the string contains only alphabets.isdigit(): Checks if the string contains only digits (0–9).islower(): Checks if all letters in the string are lowercase.isupper(): Checks if all letters in the string are uppercase.isspace(): Checks if the string contains only whitespace.startswith(): Checks if the string starts with a specific substring.endswith(): Checks if the string ends with a specific substring.
Replace and Strip Functions
replace(old, new)
👉 Definition: Replaces occurrences of the old substring with the new one.
👉 Example: "hello".replace("l","x") # "hexxo"
strip()
👉 Definition: Removes leading and trailing whitespace.
lstrip() / rstrip()
👉 Definition: Removes whitespace from the left side (lstrip) or right side (rstrip) only.
Split and Join
split()
👉 Definition: Splits a string into a list based on a separator.
👉 Example: "a,b,c".split(",") # ['a','b','c']
join()
👉 Definition: Joins list elements into a string using a specified separator.
👉 Example: ",".join(['a','b']) # "a,b"
Python List Built-in Functions
Adding Elements
append(x)
👉 Definition: Adds a single element to the end of the list.
extend(iterable)
👉 Definition: Adds all elements of an iterable to the end of the list.
insert(i, x)
👉 Definition: Inserts an element at a specific index.
Removing Elements
remove(x)
👉 Definition: Removes the first occurrence of the specified element.
pop([i])
👉 Definition: Removes and returns the element at the given index (or the last element if no index is provided).
clear()
👉 Definition: Removes all elements from the list.
Sorting and Reversing
sort()
👉 Definition: Sorts the list in ascending order in-place.
reverse()
👉 Definition: Reverses the order of elements in the list.
General Utility Functions
len(): Returns the total count of elements.max(): Returns the largest element.min(): Returns the smallest element.sum(): Returns the total of all numeric elements.
