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

Updated on: 07-Dec-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements