Programming Articles - Page 3356 of 3366

How to check if a string only contains certain characters in Python?

Sarika Singh
Updated on 02-Sep-2025 13:21:55

9K+ Views

To check if a Python string contains only certain characters, you can use - set Comparison Regular Expressions Character Lists These approaches help you to verify whether every character in the string belongs to a defined set of allowed characters or not. Using Set Comparison You can create a set of allowed characters and check if all characters in the string belong to that set or not. If the string is a subset of the allowed set, it means the string contains only valid (accepted) characters. Example In the following example, we define a set of allowed numeric ... Read More

How to check if a Python string contains only digits?

Sarika Singh
Updated on 02-Sep-2025 13:15:29

12K+ Views

To check if a Python string contains only digits, you can use the built-in isdigit() method. This method returns True if all characters in the string are digits, and False otherwise. Besides isdigit(), you can also use loops or regular expressions to verify this condition. Using the isdigit() Method The isdigit() method is used to check if a string contains only digits. It returns True only when every character is a digit. Example In this example, we define a string that contains only digits and use the isdigit() method to check it. The output confirms that the string contains only ... Read More

What is the Python regular expression to check if a string is alphanumeric?

Sarika Singh
Updated on 02-Sep-2025 13:22:21

6K+ Views

In this article, we focus on how to check if a string is alphanumeric using regular expressions in Python. Regular expressions are very useful for pattern matching and validation. To use them, first import the re library, which is included by default in Python. The regular expression ^[a-zA-Z0-9]+$ matches strings that contain only letters (both uppercase and lowercase) and numbers. This expression returns True if the string is alphanumeric; otherwise, it returns False. Using Regular Expressions By applying the re.match() function with the above pattern, you can check if the entire string contains only alphanumeric characters. Example: Checking an Alphanumeric ... Read More

How to check if a string is alphanumeric in Python?

Sarika Singh
Updated on 09-Jun-2025 09:19:01

7K+ Views

In Python, strings can contain letters, numbers, or special characters. To check if a string is alphanumeric (contains only letters and numbers), we can use different methods. This article shows three simple ways to do that: Using the isalnum() function Using regular expressions Using the isalpha() and isdigit() functions Using the isalnum() Function The isalnum() method returns True if all characters in the string are letters or digits; otherwise, it returns False. Example: Basic Alphanumeric Check In the example below, we take two strings and check if they contain only alphabets and numbers using the isalnum() function: str1 ... Read More

How do I verify that a string only contains letters, numbers, underscores and dashes in Python?

Sarika Singh
Updated on 02-Sep-2025 13:22:52

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

How to check if a string has at least one letter and one number in Python?

Sarika Singh
Updated on 02-Sep-2025 13:23:14

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

How do I check if a string has alphabets or numbers in Python?

Sarika Singh
Updated on 02-Sep-2025 13:15:53

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

How to pass arguments by reference in a Python function?

Sarika Singh
Updated on 02-Sep-2025 13:14:26

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

How to implement a custom Python Exception with custom message?

Manogna
Updated on 13-Feb-2020 05:11:20

295 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

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

Rajendra Dharmkar
Updated on 02-Jun-2025 17:30:08

1K+ 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

Advertisements