Python Common Helper Functions

Sumanta Mukhopadhyay
2 min readFeb 14, 2023

--

Python is a popular programming language known for its simplicity and ease of use. It offers a wide range of libraries and functions that make programming more efficient and effective. In this article, we will cover 36 commonly used helper functions in Python and how they can be used to simplify your code.

String Manipulation:

  1. str.replace() - replaces a substring with another substring in a string
  2. str.upper() - returns the uppercase version of a string
  3. str.lower() - returns the lowercase version of a string
  4. str.strip() - removes whitespace from the beginning and end of a string
  5. str.split() - splits a string into a list of substrings based on a delimiter
  6. str.join() - concatenates a list of strings into a single string with a delimiter
  7. str.startswith() - checks if a string starts with a specific substring
  8. str.endswith() - checks if a string ends with a specific substring
  9. str.find() - returns the index of the first occurrence of a substring in a string
  10. str.count() - returns the number of occurrences of a substring in a string

List Manipulation:

  1. len() - returns the length of a list
  2. list.append() - adds an item to the end of a list
  3. list.extend() - adds the elements of a list to another list
  4. list.insert() - inserts an item at a specific index in a list
  5. list.pop() - removes and returns the last item in a list
  6. list.remove() - removes the first occurrence of an item from a list
  7. list.index() - returns the index of the first occurrence of an item in a list
  8. list.sort() - sorts the items in a list in ascending order
  9. list.reverse() - reverses the order of the items in a list

Math:

  1. max() - returns the maximum value in a list or set of values
  2. min() - returns the minimum value in a list or set of values
  3. abs() - returns the absolute value of a number
  4. round() - rounds a number to a specified number of decimal places
  5. pow() - raises a number to a specified power
  6. sum() - returns the sum of all the values in a list or set of values

File I/O:

  1. open() - opens a file for reading or writing
  2. read() - reads the contents of a file
  3. write() - writes data to a file
  4. close() - closes a file
  5. with open() - opens a file and automatically closes it when done

Datetime:

  1. datetime.datetime.now() - returns the current date and time
  2. datetime.timedelta() - creates a time duration
  3. strftime() - formats a datetime object as a string

Random:

  1. random.randint() - returns a random integer within a specified range
  2. random.choice() - returns a random item from a list
  3. random.shuffle() - shuffles the items in a list in random order

Note: This is not an exhaustive list and there are many other helper functions available in Python for different purposes.

--

--

Sumanta Mukhopadhyay
Sumanta Mukhopadhyay

No responses yet