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 import data from csv to jupyter notebook?

Leave a Reply

Your email address will not be published.

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