Built in Functions
Built-in String Functions and Example Program
1. `find(substring [, start [, end]])`: Searches for the first occurrence of a substring
within a string and returns its index. Returns -1 if the substring is not found.
s = "Python Programming"
index = s.find("Programming")
print(index)
# Output: 7
Return type: `int`
2. `rfind(substring [, start [, end]])`: Similar to `find()`, but searches for the last
occurrence of the substring within the string.
s = "Python Programming"
index = s.rfind("o")
print(index)
# Output: 9
Return type: `int`
3. `index(substring [, start [, end]])`: Similar to `find()`, but raises a ValueError if
the substring is not found.
s = "Python Programming"
index = s.index("Programming")
print(index)
# Output: 7
Return type: `int`
4. `rindex(substring [, start [, end]])`: Similar to `index()`, but searches for the last
occurrence of the substring within the string.
s = "Python Programming"
index = s.rindex("o")
print(index)
# Output: 9
Return type: `int`
5. `count(substring [, start [, end]])`: Counts the occurrences of a substring within a
string.
s = "Python Programming"
count = s.count("P")
print(count)
# Output: 2
Return type: `int`
6. `replace(old, new [, count])`: Replaces occurrences of a substring with another
substring.
s = "Python Programming"
new_s = s.replace("Python", "Java")
print(new_s)
# Output: Java Programming
Return type: `str`
7. `split([sep [, maxsplit]])`: Splits a string into a list of substrings based on a delimiter.
s = "Python Programming"
parts = s.split(" ")
print(parts)
# Output: ['Python', 'Programming']
Return type: `list`
8. `join(iterable)`: Joins elements of an iterable into a string using the string as a
separator.
parts = ['Python', 'Programming']
s = " ".join(parts)
print(s)
# Output: Python Programming
Return type: `str`
9. `strip([chars])`: Removes leading and trailing whitespace (or specified characters)
from a string.
s = " Python Programming "
clean_s = s.strip()
print(clean_s)
# Output: "Python Programming"
Return type: `str`
10. Character type checking functions:
- `isalnum()`: Checks if all characters in the string are alphanumeric.
- `isalpha()`: Checks if all characters in the string are alphabetic.
- `isdigit()`: Checks if all characters in the string are digits.
- `islower()`: Checks if all characters in the string are lowercase.
- `isupper()`: Checks if all characters in the string are uppercase.
- `isspace()`: Checks if all characters in the string are whitespace.
Example:
s = "PythonProgramming123"
print(s.isalnum()) # Output: True
print(s.isalpha()) # Output: False
print(s.isdigit()) # Output: False
print(s.islower()) # Output: False
print(s.isupper()) # Output: False
print(s.isspace()) # Output: False
Return type: `bool`
Built-in List Functions and Example Program
1. `count(value)`: Counts the number of occurrences of a value in a list.
my_list = [1, 2, 3, 4, 1, 2, 1]
count = my_list.count(1)
print(count)
# Output: 3
Return type: `int`
2. `index(value [, start [, end]])`: Returns the index of the first occurrence of a value in a
list.
my_list = [1, 2, 3, 4, 1, 2, 1]
index = my_list.index(2)
print(index)
# Output: 1
Return type: `int`
3. `remove(value)`: Removes the first occurrence of a value from a list.
my_list = [1, 2, 3, 4, 1, 2, 1]
my_list.remove(2)
print(my_list)
# Output: [1, 3, 4, 1, 2, 1]
Return type: `None`
4. `pop([index])`: Removes and returns the element at the specified index. If no index is
specified, removes and returns the last element.
my_list = [1, 2, 3, 4, 5]
element = my_list.pop(2)
print(element)
# Output: 3
print(my_list)
# Output: [1, 2, 4, 5]
Return type: `int`
5. `reverse()`: Reverses the elements of a list in place.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)
# Output: [5, 4, 3, 2, 1]
Return type: `None`
6. `sort(key=None, reverse=False)`: Sorts the elements of a list in place.
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5]
my_list.sort()
print(my_list)
# Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
Return type: `None`
7. `append(value)`: Adds a single element to the end of a list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
# Output: [1, 2, 3, 4]
Return type: `None`
8. `extend(iterable)`: Extends the list by appending elements from the iterable.
my_list = [1, 2, 3]
other_list = [4, 5, 6]
my_list.extend(other_list)
print(my_list)
# Output: [1, 2, 3, 4, 5, 6]
Return type: `None`
9. `del`: Deletes elements from a list or deletes the entire list.
my_list = [1, 2, 3, 4, 5]
del my_list[2]
print(my_list) # Output: [1, 2, 4, 5]
Return type: `None`
Built-in Tuple Functions and Example Program
1. `count(value)`: Counts the number of occurrences of a value in a tuple.
my_tuple = (1, 2, 3, 4, 1, 2, 1)
count = my_tuple.count(1)
print(count)
# Output: 3
Return type: `int`
2. `index(value [, start [, end]])`: Returns the index of the first occurrence of a value
in a tuple.
my_tuple = (1, 2, 3, 4, 1, 2, 1)
index = my_tuple.index(2)
print(index)
# Output: 1
Return type: `int`
3. `len()`: Returns the number of elements in a tuple.
my_tuple = (1, 2, 3, 4, 5)
length = len(my_tuple)
print(length)
# Output: 5
Return type: `int`
4. `min()`: Returns the smallest element in a tuple.
my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5)
minimum = min(my_tuple)
print(minimum)
# Output: 1
Return type: Same type as elements in the tuple
5. `max()`: Returns the largest element in a tuple.
my_tuple = (3, 1, 4, 1, 5, 9, 2, 6, 5)
maximum = max(my_tuple)
print(maximum)
# Output: 9
Return type: Same type as elements in the tuple
6. `del`: Deletes the entire tuple.
my_tuple = (1, 2, 3, 4, 5)
del my_tuple
Return type: `None`
Built-in Set Functions and Example Program
1. `add(element)`: Adds a single element to a set.
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
# Output: {1, 2, 3, 4}
Return type: `None`
2. `remove(element)`: Removes a specified element from a set. Raises a KeyError if
the element is not present.
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set)
# Output: {1, 2, 4}
Return type: `None`
3. `discard(element)`: Removes a specified element from a set if it is present.
Does not raise an error if the element is not found.
my_set = {1, 2, 3, 4}
my_set.discard(5)
print(my_set)
# Output: {1, 2, 3, 4}
Return type: `None`
4. `pop()`: Removes and returns an arbitrary element from a set. Raises a
KeyError if the set is empty.
my_set = {1, 2, 3, 4}
element = my_set.pop()
print(element)
# Output: 1 or any other element
Return type: Same type as the elements in the set
5. `clear()`: Removes all elements from a set.
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set)
# Output: set()
Return type: `None`
6. `copy()`: Returns a shallow copy of a set.
my_set = {1, 2, 3, 4}
new_set = my_set.copy()
print(new_set)
# Output: {1, 2, 3, 4}
Return type: `set`
7. `union(*others)`: Returns a new set containing the union of the original set and
other sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)
# Output: {1, 2, 3, 4, 5}
Return type: `set`
8. `intersection(*others)`: Returns a new set containing the intersection of the
original set and other sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
# Output: {3}
Return type: `set`
Built-in Dictionary Functions and Example Program
1. `clear()`: Removes all elements from a dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict)
# Output: {}
Return type: `None`
2. `copy()`: Returns a shallow copy of a dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = my_dict.copy()
print(new_dict)
# Output: {'a': 1, 'b': 2, 'c': 3}
Return type: `dict`
3. `keys()`: Returns a view of all keys in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
print(keys)
# Output: dict_keys(['a', 'b', 'c'])
Return type: `dict_keys`
4. `values()`: Returns a view of all values in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
values = my_dict.values()
print(values)
# Output: dict_values([1, 2, 3])
Return type: `dict_values`
5. `items()`: Returns a view of all key-value pairs in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
items = my_dict.items()
print(items)
# Output: dict_items([('a', 1), ('b', 2), ('c', 3)])
Return type: `dict_items`
6. `get(key [, default])`: Returns the value associated with a specified key. If the key is
not found, returns the default value or None if not specified.
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.get('b')
print(value)
# Output: 2
Return type: Same type as the values in the dictionary or the
specified default value.
7. `pop(key [, default])`: Removes and returns the value associated with a specified key.
If the key is not found, returns the default value or raises a KeyError if not specified.
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b')
print(value)
# Output: 2
Return type: Same type as the values in the dictionary or the
specified default value.