Function
Function
Converts a value to
str() str(123) '123'
string
len() Returns length of string len("hello") 5
lower() Converts to lowercase "HELLO".lower() 'hello'
upper() Converts to uppercase "hello".upper() 'HELLO'
'Hello
capitalize() Capitalizes first letter "hello world".capitalize()
world'
'Hello
title() Capitalizes each word "hello world".title()
World'
Removes
strip() " hello ".strip() 'hello'
leading/trailing spaces
lstrip() Removes leading spaces " hello".lstrip() 'hello'
rstrip() Removes trailing spaces "hello ".rstrip() 'hello'
"hello world".replace("world", 'hello
replace(old, new) Replaces substring
"Python") Python'
Splits string by
split(sep) "a,b,c".split(",") ['a', 'b', 'c']
separator
Joins elements with
join(iterable) ".".join(['a', 'b', 'c']) 'a.b.c'
string
Finds index of first
find(sub) "hello".find("l") 2
match
rfind(sub) Finds last match index "hello".rfind("l") 3
Like find(), but error if
index(sub) "hello".index("e") 1
not found
count(sub) Counts occurrences "banana".count("a") 3
Checks if string starts
startswith(prefix) "hello".startswith("he") True
with prefix
Checks if ends with
endswith(suffix) "hello".endswith("lo") True
suffix
isalpha() Checks all letters "hello".isalpha() True
isdigit() Checks all digits "12345".isdigit() True
Checks letters and
isalnum() "abc123".isalnum() True
numbers
isspace() Checks for only spaces " ".isspace() True
islower() Checks all lowercase "hello".islower() True
isupper() Checks all uppercase "HELLO".isupper() True
swapcase() Swaps case "Hello".swapcase() 'hELLO'
STRING FUNCTIONS TO REMEMBER:-
TUPLE FUNCTIONS:-
Functio
What it Does Example Output
n
len() Returns number of elements len((1, 2, 3)) 3
count(x) Counts how many times x appears (1, 2, 1, 3).count(1) 2
index(x) Returns index of first occurrence of x (10, 20, 30).index(20) 1
max() Returns maximum value max((1, 5, 3)) 5
min() Returns minimum value min((1, 5, 3)) 1
sum() Returns the sum of elements sum((1, 2, 3)) 6
sorted() Returns sorted list of tuple elements sorted((3, 1, 2)) [1, 2, 3]
tuple() Converts iterable to tuple tuple([1, 2, 3]) (1, 2, 3)
DICTIONARY FUNCTIONS:-