- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34494 Articles for Programming
![Tarun Chandra](https://www.tutorialspoint.com/assets/profiles/538136/profile/60_1048426-1666076525.jpeg)
4K+ Views
In this article, we are going to find out how to check if a string has at least one letter and one number in Python. Regular expressions is used in the first technique. Import the re library and install it if it isn't already installed to use it. After importing the re library, we can use the regular expression ('^(?=.*[0-9]$)(?=.*[a-zA-Z])'. This will return False if the string contains any special characters other than Alphabets and Numbers; otherwise, True will be returned. In regular expressions, the ?= syntax is used to call lookaheads. Lookaheads discover matches in the provided string by ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
2K+ Views
To check if a string has alphabets or numbers in Python, we can use some built-in methods like isalpha() and isdigit(). isalpha() returns True if all characters in a string are alphabets (letters), and False otherwise. isdigit() returns True if all characters in a string are digits (numbers), and False otherwise. Here are two examples that demonstrate how to check if a string has alphabets or numbers: To Check if a String has Alphabets or Numbers Example In this example, we first prompt the user to enter a string using the input() function. We then check if the string ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
578 Views
In Python, you cannot pass arguments by reference like you can in other programming languages like C++. However, there is a way to simulate pass by reference behavior in Python using mutable data types like lists and dictionaries. In Python, arguments are passed by assignment, which means that the parameter name in the function definition becomes a reference to the object passed as an argument. Therefore, there is no direct way to pass arguments by reference in Python. However, you can achieve a similar effect by passing a mutable object like a list or a dictionary to a function ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
208 Views
For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value must be within 1 and 10. Process finished with exit code 0
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
362 Views
A suffix is defined as a letter or group of letters added at the end of a word to make a new word. Let's say you have a list of suffixes, which are groups of letters that can be added to the end of a word to change its meaning. You want to check if a given string ends with one of these suffixes in Python. To check if a string ends with a suffix in a list Example In this example, we first define a list of suffixes that we want to check if the string ends with. ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
1K+ Views
To check if a string or a substring of a string ends with a suffix in Python, there are several ways to accomplish this. Here are some examples: Using the endswith() Method The endswith() method checks if the string ends with a specified suffix and returns a boolean value. To check if a substring of a string ends with a suffix, you can use string slicing to get the substring and then apply endswith() on it. Example string = "hello world" suffix = "world" if string.endswith(suffix): print("String ends with suffix") Output String ends with ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
738 Views
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 as bugs. The process of finding and removing errors is called debugging. Errors can be categorized into three major groups:Syntax errors 2. Runtime errors and 3. Logical errorsSyntax errorsPython will find these kinds of errors when it tries to parse your program, and exit with an error message without running ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
811 Views
We can write custom exception classes with error codes and error messages as follows:class ErrorCode(Exception): def __init__(self, code): self.code = code try: raise ErrorCode(401) except ErrorCode as e: print "Received error with code:", e.codeWe get outputC:/Users/TutorialsPoint1/~.py Received error with code: 401We can also write custom exceptions with arguments, error codes and error messages as follows:class ErrorArgs(Exception): def __init__(self, *args): self.args = [a for a in args] try: raise ErrorArgs(403, "foo", "bar") except ErrorArgs as e: print "%d: %s ... Read More
![Manogna](https://www.tutorialspoint.com/assets/profiles/11051/profile/60_103575-1512718394.jpg)
118 Views
We can use the finally clause to clean up whether an exception is thrown or not:try: #some code here except: handle_exception() finally: do_cleanup()If the cleanup is to be done in the event of an exception, we can code like this:should_cleanup = True try: #some code here should_cleanup = False except: handle_exception() finally: if should_cleanup(): do_cleanup()