import random
import string
'''
随机生成一个字符串
字符串长度: 自定义
'''classStringGenerator:def__init__(self, length):
self.length = length
defgenerate_string(self):if self.length <=0:return"Length should be greater than zero."else:return''.join(random.choices(string.ascii_letters + string.digits, k=self.length))# 示例用法if __name__ =="__main__":try:
input_length =int(input("Enter the desired length for the string: "))
generator = StringGenerator(input_length)
result_string = generator.generate_string()print(f"Generated string: {result_string}")print(f"Length of the string: {len(result_string)}")except ValueError:print("Invalid input. Please enter a valid integer for the length.")