Text data can contain alphabets numbers, spaces and special characters such as and

Improve Article

Save Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Given a string, we have to find whether the string contains any letters, numbers, underscores, and dashes or not. It is usually used for verifying username and password validity. For example, the user has a string for the username of a person and the user doesn’t want the username to have any special characters such as @, $, etc. 

    Prerequisite: Regular Expression in Python

    Let’s see the different methods for solving this task:

    Method 1: Using Regular Expression.

    There is a function in the regular expression library(re) that compares two strings for us. re.match(pattern, string) is a function that returns an object, to find whether a match is find or not we have to typecast it into boolean. 

    Syntax: 

    re.match(pattern, string) 

    Parameters:

    • pattern: the pattern against which you want to check 
    • string: the string you want to check for the pattern 

    Return: Match object 

    Let’s see an example:

    Example 1:

    Python3

    import re

    pattern = "^[A-Za-z0-9_-]*$"

    string = "G33ks_F0r_Geeks"

    state = bool(re.match(pattern, string))

    print(state)

    Output:

    True

    Example 2:

    Python3

    import re

    print(bool(re.match("^[A-Za-z0-9_-]*$",

                        'ValidString12-_')))

    print(bool(re.match("^[A-Za-z0-9_-]*$",

                        'inv@lidstring')))

    Output:

    True
    False

    Method 2: Using Set.

    Set is a built-in data type in Python. We are usingissubset()function of a set that returns True if all characters of a set are present in a given set Otherwise False.

    Syntax: 

    set_name.issubset(set)

    Parameters: 

    • set: Represents that set in which the subset has to be searched 

    Return: boolean value

    Let’s see an example:

    Example 1: 

    Python3

    allowed_chars = set(

        ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"))

    string = "inv@lid"

    validation = set((string))

    if validation.issubset(allowed_chars):

        print("True")

    else:

        print("False")

    Output:

    False

    Example 2: 

    Python3

    allowed_chars = set(

        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-")

    string = "__Val1d__"

    validation = set((string))

    if validation.issubset(allowed_chars):

        print("True")

    else:

        print("False")

    Output:

    True

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O(n)

    Auxiliary Space: O(n)

    Method : Using replace() and len() methods

    Python3

    allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"

    string = "__Val1d__"

    for i in allowed_chars:

        string = string.replace(i, "")

    if len(string) == 0:

        print("True")

    else:

        print("False")


    Which data type contains letters numbers and symbols?

    The CHAR data type stores any string of letters, numbers, and symbols. It can store single-byte and multibyte characters, based on the database locale. The CHARACTER data type is a synonym for CHAR.

    Which data in MS Excel can contain alphabets numbers spaces and special characters?

    Text data is the combination of letters, special characters, symbols, spaces and numbers.

    What is the data which include alphabets combination of alphabets spaces and numbers called?

    Alphanumeric, also referred to as alphameric, is a term that encompasses all of the letters and numerals in a given language set. In layouts designed for English language users, alphanumeric characters are those comprised of the combined set of the 26 alphabetic characters, A to Z, and the 10 Arabic numerals, 0 to 9.

    Can hold data which can be letters or numbers?

    One of the most widely used data types is a string. A string consists of one or more characters, which can include letters, numbers, and other types of characters.