Sarika Singh

Sarika Singh

137 Articles Published

Articles by Sarika Singh

Page 3 of 14

How to check if text is “empty” (spaces, tabs, newlines) in Python?

Sarika Singh
Sarika Singh
Updated on 11-Jun-2025 5K+ Views

In this article, we are going to focus on how to check if the text is empty (spaces, tabs, newlines) in Python. Checking if text is Empty using isspace() method The isspace() method determines whether a string contains only spaces or contains any other characters. If the given string is only made up of spaces, this method returns true; otherwise, it returns false. Even if the string contains characters like t and n, the method returns true. Example In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the isspace() ...

Read More

Python Program for 0-1 Knapsack Problem

Sarika Singh
Sarika Singh
Updated on 10-Jun-2025 6K+ Views

Knapsack is a classic problem that involves decision-making based on constraints. Suppose we have a knapsack with fixed capacity, and items with fixed values and sizes (it might be volume of the item or weight). We need to add items into the knapsack such that we pack the maximum value items. From the available combinations, we need to provide an optimized solution. There are three types of knapsack problems 0-1 Knapsack: We can either pick an item or not (0 or 1). Fractional Knapsack: We can pick a part of an ...

Read More

What are different data conversion methods in Python?

Sarika Singh
Sarika Singh
Updated on 09-Jun-2025 1K+ Views

Data conversion in Python refers to changing the data type of a value to another data type. Python supports two different types of data conversion, such as Implicit conversion and explicit conversion. Implicit Conversion is performed by the Python interpreter automatically. Explicit Conversion is manually done by the programmer using built-in functions in Python Implicit Conversion Python automatically converts one data type to another, and this type of conversion is called Implicit conversion. To avoid any data loss during runtime, the smaller data type is converted ...

Read More

How to join two strings to convert to a single string in Python?

Sarika Singh
Sarika Singh
Updated on 09-Jun-2025 2K+ Views

In Python, we can join two strings into one single string using different ways, such as - Using the + Operator Using the join() Method Using Formatted Strings String Concatenation in Loops Let us look at each method with examples. Using + Operator The most common way to combine two strings into one in Python is by using the + operator. It joins the strings exactly as they are, without any space or separator in between. Example In the following example, we ...

Read More

How to check if a character is upper-case in Python?

Sarika Singh
Sarika Singh
Updated on 09-Jun-2025 44K+ Views

In this article, we are going to find out how to check if a character is an uppercase letter in Python. We will explore four different approaches - Using the isupper() method Using regular expressions Using ASCII values Direct comparison In each approach, we will check whether a given character is in uppercase and return True or False accordingly. Using the isupper() Method The first and most straightforward way is by using the built-in isupper() method. This method returns True if the ...

Read More

How to catch an exception while using a Python \\\\\\\'with\\\\\\\' statement?

Sarika Singh
Sarika Singh
Updated on 08-Jun-2025 272 Views

In Python, the with statement is used when working with files or network connections. It makes sure that resources (files) are opened, used, and then closed properly, even if an error occurs during the process. If you want to catch any exceptions that happen inside a with block, you can wrap it in a try-except statement. Alternatively, you can use a custom context manager that handles exceptions on its own. Catching Exceptions Inside with Block Using try-except To catch errors that occur inside a with block, we need to place it within a try-except. This helps you to handle any ...

Read More

Are Python Exceptions runtime errors?

Sarika Singh
Sarika Singh
Updated on 07-Jun-2025 1K+ Views

Are Python Exceptions Runtime Errors? Yes, Python exceptions are considered runtime errors. Most exceptions occur during runtime.Exceptions in Python are errors that occur when there is an abnormal scenario during the execution of a program, which terminates the execution abruptly, interrupting its normal flow. Examples of exceptions are ZeroDivisionError, IndexError,  ValueError, etc..Whereas, errors in Python are detected before the execution of the program, they include syntactical errors and other violations of the program rules. Examples of errors are SyntaxError,  IndentationError,  etc.  Exceptions are Raised at Runtime Exceptions occur when the execution of the program is interrupted because of issues like logical ...

Read More

How to catch FloatingPointError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 07-Jun-2025 2K+ Views

Catching FloatingPointError Exception in PythonFloatingPointError in Python is an exception that occurs when there is an error in floating-point calculations. By default, Python does not raise this error for basic operations like dividing by zero; instead, it returns inf or nan. To catch this error, you need to enable it explicitly using the numpy module. In this article, you will learn how to catch a FloatingPointError by enabling it through NumPy settings and handling it using a try-except block. When Does FloatingPointError Occur? FloatingPointError can occur in cases like - Divide by zero in floating-point calculations (if enabled) ...

Read More

How to catch ImportError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 07-Jun-2025 4K+ Views

The ImportError exception in Python is raised when the interpreter cannot find or load a module that is being imported using the import statement.  Catching the ImportError ExceptionLike any other exception, we can catch the ImportError exception using the try-except blocks. In this article, we discuss various scenarios where the ImportError exception occurs, with examples, and catch the generated exception using the try-except blocks in each scenario.When Does ImportError Occur?The ImportError generally occurs in the following cases -Trying to import a module that doesn't exist.Misspelling the module name.Trying to import a function or class that is not available in the specified module.Example: Importing a ...

Read More

How to catch ValueError using Exception in Python?

Sarika Singh
Sarika Singh
Updated on 07-Jun-2025 466 Views

While passing arguments to a Python function, if the datatype of the given values is different from the function parameters, a TypeError is raised. But if the type of the argument is accurate and its value is inappropriate, a ValueError exception is raised In this article, you will learn how to catch ValueError exceptions using the general Exception class, which can catch all built-in exceptions, including ValueError. The ValueError exception commonly occurs when - You convert a string to an integer or float, but the string is not valid You pass an invalid value ...

Read More
Showing 21–30 of 137 articles
« Prev 1 2 3 4 5 14 Next »
Advertisements