Created
January 4, 2023 09:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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