Skip to content

Generate random string of given length in Python

Advertisements
import random
import string


# Generate a random string of a given length
def gen_random_string_of_length(length):
    return "".join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.ascii_letters + string.digits + string.punctuation) for i in range(length))


def main():
    # Generate a random string of length n
    print(gen_random_string_of_length(10))


main()

See also  Python code snippet - How to create a venv?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.