Found 27759 Articles for Server Side Programming

How to check if a substring is contained in another string in Python?

Rajendra Dharmkar
Updated on 10-Aug-2023 18:37:32

229 Views

A substring is a contiguous sequence of characters within a string. In other words, it's a smaller piece of text that appears within a larger piece of text. For example, in the string "lorem ipsum", the substring "lorem" appears within the larger string. Similarly, the substring "em i" appears within the larger string "lorem ipsum" as well. Substring operations are common in programming and can be used for various tasks such as searching for a specific word in a text, extracting parts of a text, or replacing certain parts of a text with other words. Checking if a substring is ... Read More

How to catch KeyError Exception in Python?

Manogna
Updated on 13-Feb-2020 04:59:45

683 Views

A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.Exampleimport sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())Output(, KeyError('c',), )

What is the use of "assert" statement in Python?

Manogna
Updated on 27-Sep-2019 08:04:22

181 Views

The assert statement has the following syntax.assert , The line above is read as: If evaluates to False, an exception is raised and will be output.If we want to test some code block or an expression we put it after an assert keyword. If the test passes or the expression evaluates to true nothing happens. But if the test fails or the expression evaluates to false, an AssertionError is raised and the message is printed out or evaluated.Assert statement is used for catching/testing user-defined constraints. It is used for debugging code and is inserted at the start of ... Read More

Explain try, except and finally statements in Python.

Manogna
Updated on 13-Feb-2020 05:03:24

423 Views

In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.If an exception occurs, the rest of the try block is skipped and the except clause is executed.Exampletry: 'apple' + 6 except Exception: print "Cannot concatenate 'str' and 'int' objects"OutputCannot concatenate 'str' and 'int' objectsWe avoid the traceback error message elegantly with a simple message like above by using try except statements for exception handling.In addition to using an except block after the try block, we can also use the finally block. The ... Read More

What is unexpected indent in Python?\\\

Manogna
Updated on 26-Sep-2019 19:56:53

420 Views

Python not only insists on indentation, it insists on consistent indentation. If we indent one line by 4 spaces, but then if we indent the next by 3 (or 5, 6, .), we get this error of unexpected indent in python.In the given code, line 3 has more spaces at the start than line 2. All lines of code in a block must start with exactly the same number of spaces. Both print statements must be indented same number of spaces. So the corrected code that does not show unexpected indent is as follows.def a():     print "foo"     print "baz"

What is the difference between 'except Exception as e' and 'except Exception, e' in Python?

Rajendra Dharmkar
Updated on 25-Jun-2020 20:57:23

873 Views

The difference between using ', ' and 'as' in except statements, is as follows:Both ', ' and 'as' are same functionality wise; but their use depends on the python versions as follows.In Python 2.5 and earlier versions, use of the 'comma' is recommended since 'as' isn't supported.In Python 2.6+ versions, both 'comma' and 'as' can be used. But from Python 3.x, 'as' is required to assign an exception to a variable.As of Python 2.6 using 'as' allows us an elegant way to catch multiple exceptions in a single except block as shown belowexcept (Exception1, Exception2) as erris any day better ... Read More

How to ignore an exception and proceed in Python?

Alekhya Nagulavancha
Updated on 22-Feb-2023 17:56:49

23K+ Views

An exception is an unexpected error or event that occurs during the execution of program. The difference between an error and an exception in a program is that, when an exception is encountered, the program deflects from its original course of execution whereas when an error occurs, the program is terminated. Hence, unlike errors, an exception can be handled. Therefore, you won't have a program crash. However, in some cases of Python, the exception might not cause the program to terminate and will not affect the direction of execution hugely. Therefore, it is best to ignore such kind of exceptions. ... Read More

How to handle a python exception within a loop?

Alekhya Nagulavancha
Updated on 22-Feb-2023 17:54:33

9K+ Views

The looping technique in Python transforms complex problems into simple ones. It allows us to change the flow of the program so that instead of writing the same code over and over, we can repeat it a limited number of times until a certain condition is satisfied. For example, if we need to display the first ten natural numbers, we can do it inside a loop that runs up to ten iterations rather than using the print command ten times. Python offers three ways to loop a block of code in a program: using for loops, while loops and nested ... Read More

How to catch a python exception in a list comprehension?

Alekhya Nagulavancha
Updated on 04-Apr-2023 13:38:19

2K+ Views

Before we understand how to catch a Python exception in a list comprehension, let us first learn what a list comprehension is. List comprehension is a statement that allows you to create a list and execute a for loop, all in a single sentence. This also allows the lists to be created from other iterables like tuples, strings, arrays, lists, and so on. The syntax of list comprehension is given below − List = [expression(element) for element in list if condition] The python list and list comprehension capability, which may be used inside a single line of code to ... Read More

Where's the standard python exception list for programmers to raise?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

82 Views

I want to know what all standard exceptions are there in python. Where can I find a list of standard python exceptions?The standard python exception list for programmers is available athttps://docs.python.org/3/library/exceptions.html

Advertisements