Skip to content

Instantly share code, notes, and snippets.

@deangrant
Created January 4, 2023 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deangrant/6c5572be0cc0bee65cf5ce22e96df9b1 to your computer and use it in GitHub Desktop.
Save deangrant/6c5572be0cc0bee65cf5ce22e96df9b1 to your computer and use it in GitHub Desktop.
Generate a string of the specified length with randomly chosen characters from the characters string. It will also ensure that the string contains at least one uppercase letter and one number from the required_characters string.
import random
# Set the length of the string
length = 8
# Set the characters that the string can contain
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# Set the required characters
required_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# Generate the string
string = ""
while len(string) < length:
string += random.choice(characters)
if any(char in string for char in required_characters):
break
print(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment