0% found this document useful (0 votes)
5 views

Function

The document provides a comprehensive overview of various string, list, tuple, and dictionary functions in Python, detailing their functionalities, examples, and outputs. Each function is listed with a brief description of what it does, along with sample code and expected results. This serves as a quick reference guide for Python programming.

Uploaded by

yiwav67616
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Function

The document provides a comprehensive overview of various string, list, tuple, and dictionary functions in Python, detailing their functionalities, examples, and outputs. Each function is listed with a brief description of what it does, along with sample code and expected results. This serves as a quick reference guide for Python programming.

Uploaded by

yiwav67616
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function What it Does Example Output

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:-

LIST FUNCTIONS TO REMEMBER !

Function What it Does Example Output


len() Returns the number of elements len([1, 2, 3]) 3
append(x) Adds element x to end lst.append(4) [1, 2, 3, 4]
Function What it Does Example Output
insert(i, x) Inserts x at index i lst.insert(1, 99) [1, 99, 2, 3]
extend(iterable) Adds all elements of iterable lst.extend([4, 5]) [1, 2, 3, 4, 5]
remove(x) Removes first occurrence of x lst.remove(2) [1, 3]
Removes and returns element at 3 (if list was [1,
pop(i) lst.pop()
index i (default last) 2, 3])
clear() Removes all elements lst.clear() []
Returns index of first occurrence
index(x) lst.index(3) 2
of x
Returns number of times x
count(x) lst.count(2) 1
appears
sort() Sorts list in ascending order lst.sort() [1, 2, 3]
reverse() Reverses the list lst.reverse() [3, 2, 1]
new_lst = new_lst is same
copy() Returns a shallow copy of list
lst.copy() as lst

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:-

Function What it Does Example Output


Returns number of key-
len() len({'a':1, 'b':2}) 2
value pairs
keys() Returns all keys d.keys() dict_keys(['a', 'b'])
values() Returns all values d.values() dict_values([1, 2])
Function What it Does Example Output
dict_items([('a', 1), ('b',
items() Returns key-value pairs d.items()
2)])
get(k) Returns value for key k d.get('a') 1
Updates dictionary with
update() d.update({'c':3}) {'a':1, 'b':2, 'c':3}
another
Removes key and returns
pop(k) d.pop('b') 2
value
Removes last inserted
popitem() d.popitem() e.g. ('c', 3)
key-value pair
clear() Removes all items d.clear() {}
copy() Returns shallow copy new_d = d.copy() {'a': 1}
fromkeys( Creates new dict from dict.fromkeys(['x', 'y'],
{'x': 0, 'y': 0}
) keys with same value 0)

You might also like