Rajendra Dharmkar has Published 784 Answers

Are Python Exceptions runtime errors?

Rajendra Dharmkar
Published on 06-Dec-2017 11:06:40
All python exceptions are not runtime errors, some are syntax errors as well.If you run the given code, you get the following output.File "C:/Users/TutorialsPoint1/~.py", line 4 else: ^ SyntaxError: invalid syntaxWe see that it is syntax error and not a runtime error.Errors or inaccuracies in a program are often called ... Read More

How to handle python exception inside if statement?

Rajendra Dharmkar
Published on 06-Dec-2017 10:53:49
The code can be written as follows to catch the exceptiona, b=5, 0 try:    if b != 0:        print a/b    else:        a/b        raise ZeroDivisionError except Exception as e:        print eWe get the following outputC:/Users/TutorialsPoint1/~.py integer division ... Read More

How to check if string or a substring of string ends with suffix in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 10:50:57
Python has a method endswith(string) in the String class. This method accepts a suffix string that you want to search and is called on a string object. You can call this method in the following way:string = 'C:/Users/TutorialsPoint1/~.py' print(string.endswith('.py'))OUTPUTTrueThere is another way to find if a string ends with a ... Read More

How to check whether a string ends with one from a list of suffixes in Python?

Rajendra Dharmkar
Published on 06-Dec-2017 10:48:48
Python has a method endswith(tuple) in the String class. This method accepts a tuple of strings that you want to search and is called on a string object. You can call this method in the following way:string = 'core java' print(string.endswith(('txt', 'xml', 'java', 'orld')))OUTPUTTrueThere is another way to find if ... Read More
Advertisements