Random String Digit Check

  • Share this:

Code introduction


Generates a random string of a specified length and checks if the string contains any digits.


Technology Stack : os, sys, json, re, time, random, string

Code Type : String operation

Code Difficulty : Intermediate


                
                    
import os
import sys
import json
import re
import time
import random
import string

def random_string(length=10):
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))

def xxx(arg1, arg2):
    if not isinstance(arg1, str) or not isinstance(arg2, int):
        raise ValueError("arg1 must be a string and arg2 must be an integer")
    
    if arg2 < 1:
        raise ValueError("arg2 must be greater than 0")
    
    # Generate a random string of length arg2
    random_str = random_string(arg2)
    
    # Check if the string contains any digits
    if re.search(r'\d', random_str):
        return f"The string '{random_str}' contains digits."
    else:
        return f"The string '{random_str}' does not contain digits."