
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I verify that a string only contains letters, numbers, underscores and dashes in Python?
In this article, we are going to find out how to verify a string that contains only letters, numbers, underscores, and dashes in Python.
The first strategy makes use of regular expressions. To use the re library, import it and install it if it isn't already installed. We use the regular expression "^[A-Za-z0-9_-]*$" after importing the re library.
If the string contains any special characters other than Alphabets and Numbers, this will return False; otherwise, True will be returned.
Example 1
In the example given below, we are taking a string as input and we are checking if it contains only letters, numbers, underscores, and dashes using regular expressions −
import re str1 = "Tutorialspoint123__" print("The given string is:") print(str1) print("Checking if it contains only letters, numbers, underscores, and dashes") res = bool(re.match("^[A-Za-z0-9_-]*$", str1)) print(res)
Output
The output of the above example is as shown below −
The given string is: Tutorialspoint123__ Checking if it contains only letters, numbers, underscores, and dashes True
Example 2
In the example given below, we are taking the same program as above and we are taking another string as input −
import re str1 = "Tutorialspoint@123" print("The given string is:") print(str1) print("Checking if it contains only letters, numbers, underscores, and dashes") res = bool(re.match("^[A-Za-z0-9_-]*$", str1)) print(res)
Output
The output of the above example is given below −
The given string is: Tutorialspoint@123 Checking if it contains only letters, numbers, underscores, and dashes False
Using set
The second approach is by using sets. We will declare a set with all the characters that are acceptable and we will check if the input string is a subset of the acceptable characters, if it is a subset then we will print True otherwise print False.
Example 1
In the example given below, we are taking a string as input and we are checking if it contains only letters, numbers, underscores, and dashes using sets −
acceptable_chars = set('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-') str1 = "Tutorialspoint123__" print("The given string is") print(str1) validation = set(str1) print("Checking if it contains only letters, numbers, underscores, and dashes") if validation.issubset(acceptable_chars): print(True) else: print(False)
Output
The output of the above example is as follows −
The given string is Tutorialspoint123__ Checking if it contains only letters, numbers, underscores, and dashes True
Example 2
In the example given below, we are taking the same program as above and we are taking another string as input −
acceptable_chars = set('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-') str1 = "Tutorialspoint@123" print("The given string is") print(str1) validation = set(str1) print("Checking if it contains only letters, numbers, underscores, and dashes") if validation.issubset(acceptable_chars): print(True) else: print(False)
Output
The output of the above example is given below −
The given string is Tutorialspoint@123 Checking if it contains only letters, numbers, underscores, and dashes False
- Related Articles
- How do we check in Python whether a string contains only numbers?
- How to check if a string contains only whitespace letters in Python?
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only upper case letters in Python?
- How to find If a given String contains only letters in Java?
- How do I split the letters and numbers to two arrays from a string in PHP
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters in Java
- Check if the String contains only unicode letters or digits in Java
- How do I determine if a String contains another String in Java?
- Check if the String contains only unicode letters, digits or space in Java
- How to check if a Python string contains only digits?
- Java Program to validate if a String contains only numbers
- How do I check if a string has alphabets or numbers in Python?
- How to check if a string only contains certain characters in Python?
