
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 10476 Articles for Python

13K+ Views
To verify that a string contains only letters, numbers, underscores, and dashes in Python - Use re.fullmatch() function with a pattern like [A-Za-z0-9_-]+ for regex-based checking. Use set comparison with all() function for simple logic-based validation. Many systems restrict input to certain characters for security or formatting reasons. In this case, the allowed characters are alphabets (A–Z, a–z), digits (0–9), underscores (_), and hyphens (-). Using Regular Expressions The re module in Python allows you to define patterns to validate strings. You can use re.fullmatch() function to check if the entire string matches a specific pattern, such as ... Read More

5K+ Views
To check if a string contains at least one letter and one number in Python, you can - Use any() function with str.isalpha() function and str.isdigit() function to go through characters. Use re.search() function with appropriate regular expressions for pattern matching. Both methods are commonly used for validating input strings where letters and digits are required. Using any() Function with String Methods The any() function can be combined with str.isalpha() or str.isdigit() functions to check if at least one character in the string is a letter or a digit. This approach is used for partial checks within ... Read More

4K+ Views
In Python, you can check whether a string contains letters, numbers, or both using built-in string methods such as isalpha(), isdigit(), and isalnum(). You can also use loops or regular expressions for more customized checks. Checking for Only Alphabets The isalpha() method returns True if every character in the string is a letter (a–z or A–Z) and the string is not empty. It is useful for validating names or inputs that should contain only alphabets. Example In this example, we check if the string has only alphabets using the isalpha() method - text = "HelloWorld" print(text.isalpha()) The string returns ... Read More

1K+ Views
In Python, arguments are passed to functions using call by object reference or call by sharing. This means that when you pass a variable to a function, you are actually passing a reference to the object in memory, not a separate copy. For mutable objects like lists and dictionaries, this reference allows the function to modify the original object directly. So, any changes made inside the function will be reflected outside the function as well, similar to pass-by-reference in other languages. However, for immutable objects like integers, strings, and tuples, the object itself cannot be changed. If you ... Read More

288 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

992 Views
A suffix is a group of letters added at the end of a word. In Python, we can check if a string ends with any one of multiple suffixes using the endswith() method. It takes a tuple of suffixes as an argument and returns True if the string ends with any of them. This is useful for checking file extensions, URL endings, or word patterns. Using endswith() with Multiple Suffixes The endswith() method allows you to check if a string ends with any one of several suffixes by passing them as a tuple. This helps to check for multiple ... Read More

2K+ Views
In Python, you can easily check whether a string or a substring ends with a specific suffix using the built-in endswith() method. This method returns True if the string ends with the specified suffix, otherwise it returns False. This method also allows to check multiple suffixes at once and can operate on substrings by specifying start and end positions. Using the endswith() Method The endswith() method in Python checks if a string ends with a specified suffix and returns True or False. It can also check for multiple suffixes when provided as a tuple. Following is the syntax of the ... Read More

3K+ Views
You cannot directly use an if statement to catch exceptions in Python. Instead, use a try-except block and place if conditions inside the except to respond differently based on the type or message of the exception. This allows you to write conditional logic for exception handling. Using if Inside the except Block You can write if conditions inside the except block to check the type or details of the exception and take appropriate action. Example: Matching Exception Message with "if" In this example, we catch a ValueError and use if statement to inspect its message - def process_input(value): ... Read More

964 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

1K+ Views
Custom exceptions in Python allow you to create meaningful error types that fit your application's needs. By adding error codes and error messages to custom exceptions, you can provide structured (organized) information when errors occur. Although Python has many built-in exceptions, custom exceptions are well-suited to explain specific problems in your program. Including error codes and messages, handling errors, and debugging your code. Creating a Custom Exception with Error Code You can create a custom exception by defining a class that inherits from Python's built-in Exception class. This allows you to include additional details, such as a custom error message ... Read More