How to find if a string contains the substring in Python ?

A substring is a small portion of the string. When working with strings, a common use case is to check if a given substring is present in a string or not. Python supports various built-in functions which can be used to find if a string contains the given substring or not. In today’s short tutorial let’s learn different ways of checking if a string contains the substring in Python. Note: In certain cases, the kind of strings you get to work on is not in your control. You might get None object or an empty string. Not all the methods … Read more

Pad the arrays effectively using numpy.pad()

Have you ever come across a situation where you have to add a border to an array or a tensor? Wondering how to go about it? The answer is to use the numpy.pad() The NumPy module has a function named pad() that can be used to pad certain values to the array. In this article, let us discuss the numpy.pad() function and also explore different ways of using it.  What is Padding an Array? Padding means adding some contents to the border of an array. After padding the array, the array would have a different size. But same dimensions. Have … Read more

A Detailed Guide to Remove a Key from the Dictionary in Python

Guide to remove a key from Python dictionary

Python dictionaries are one of the most used data types. These are mutable. That is, we can add new elements or remove the existing elements or change the value of elements in a Python dictionary. To remove the elements from a dictionary, we can use one of the following: pop() popitem() clear() del In today’s article, let’s look at all of these functions with an example. Recommended Reading: How to loop over Python Dictionaries? Method 1: Remove a key from the dictionary using pop() We can use the built-in pop() method to remove a key from a Python dictionary. The … Read more

How to extract div tag and its contents by id using Beautiful Soup ?

Beautiful Soup is an extensively used library in Python for web scraping. This library parses the given webpage and provides the users with an easier way to navigate and access it. When you inspect a webpage and skim through its HTML code, you’ll see that one of the most used tags are the div tags. These tags are used to make a division of the content in the webpage. If you’re scraping a blogging site, it becomes extremely important to learn how to extract the contents from div tags. In today’s article, let’s learn different ways of extracting a div … Read more

How to remove newline from Strings in Python?

In Python, \n  represents a newline character. They are also called line breaks. They are used to indicate the end of the current line. When you’re processing the input files for data cleaning, the first step is to remove the extra spaces and unnecessary newline characters from the lines. At times, you might also want to remove the new line character while printing strings. In this article, we will be addressing the following cases: Removing new line characters from a string. Removing newline characters from a list of strings. Removing newline characters while printing a list of strings. Removing newline … Read more

How to Fix json.decoder.JSONDecodeError: Expecting value

We see json.decoder.JSONDecodeError: Expecting value error when we load the json response/file with load() or loads() and the json response/file cannot be decoded by the json decoder. It can be for various reasons : The JSON response/file is empty. The JSON response/file is not in JSON format. The JSON response/file is not structured properly. The JSON response/file has values that are not understood by the json decoder. In this article, let’s see the different cases when this error is seen and possible fixes for the json.decoder.JSONDecodeError: Expecting value error. Also check the similar error, How to Fix JSONDecodeError: Expecting value: line … Read more

How to fix TypeError: ‘numpy.ndarray’ object is not callable ?

We see the TypeError: ‘numpy.ndarray’ object is not callable error in Python when we try to index the numpy.ndarray object wrongly. This is a very simple error and happens when we have just started using Numpy Array without properly knowing how to index these arrays. In this article, let’s take a look at what causes this error and determine how to overcome this error. Also check, How to print the full NumPy array without any truncation in the output? What causes TypeError: ‘numpy.ndarray’ object is not callable? In Python, we call a function using the round brackets (). To index, we … Read more

How to fix TypeError: list indices must be integers or slices not str

The TypeError: list indices must be integers or slices, not str is seen when you’re accessing the list with a string object instead of an integer object. This is a very simple error. The error statement clearly signifies the error. It says that we are indexing the list using a string object instead of an integer or slices Also check, How to fix the TypeError: list indices must be integers or slices, not a tuple. What causes the TypeError: list indices must be integers or slices, not str? In Python, lists can be indexed only with integer objects or slices. … Read more

How does the Python for loop work ?

Loops are one of the first things we learn as programmers. Especially when you are learning Python, trying out a for loop is one of the first things we’ve done. For loops in Python can be used to iterate over the elements of different data types. For example, we can iterate over strings, lists, tuples, dictionaries, and even files. Have you ever wondered how the for loop is so smart to pick letters from strings, elements from a list, or a tuple, and keys and values from a dictionary? In this article, let’s discuss in detail how the Python for … Read more

How to solve TypeError: Object of type int32 is not JSON serializable in Python

The TypeError: Object of type int32 is not JSON serializable is seen when the type of the object we pass cannot be turned into a JSON object. JSON accepts int, float type of objects. However, when you pass the int32 or int64 type of objects (data types supported by numpy) you see an error as these data types are not supported in JSON. To get rid of this error, convert the object to the int object type. In this article, let us see what causes this error and also look at ways of fixing this error. Also check, How to Fix … Read more