Created
August 29, 2016 10:55
-
-
Save kgantsov/49b4e675b16e1d004a61734e08b1a1b0 to your computer and use it in GitHub Desktop.
Generate random string in Python
This file contains hidden or 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 string | |
| from random import choice | |
| def generate_random_string(n, lowercase=True, uppercase=True, digits=True, punctuation=True): | |
| assert lowercase or uppercase or digits or punctuation, 'One of boolean params must be True' | |
| seq = ''.join([ | |
| string.ascii_lowercase if lowercase else '', | |
| string.ascii_uppercase if uppercase else '', | |
| string.digits if digits else '', | |
| string.punctuation if punctuation else '', | |
| ]) | |
| return ''.join(choice(seq) for _ in range(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment