23
loading...
This website collects cookies to deliver better user experience
string.ascii_letters
and random.choice()
for a given specified length as shown below.import random
import string
def generateRandomString(length):
# Generate mixed case alphabets string
letters = string.ascii_letters
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random string of length", length, "is:", result_str)
generateRandomString(8)
# Output
# Random string of length 8 is: hNIgRMoC
string.ascii_uppercase
and random.choice()
for a given specified length as shown below.import random
import string
def generateRandomUpperCaseString(length):
# Generate upper case alphabets string
letters = string.ascii_uppercase
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random upper case string of length", length, "is:", result_str)
generateRandomUpperCaseString(10)
# Output
# Random upper case string of length 10 is: FXFUJHAUOJ
string.ascii_lowercase
and random.choice()
for a given specified length as shown below.import random
import string
def generateRandomLowerCaseString(length):
# Generate lower case alphabets string
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random lower case string of length", length, "is:", result_str)
generateRandomLowerCaseString(10)
# Output
# Random lower case string of length 10 is: rtssovidqa
string.ascii_lowercase + string.digits
and random.choice()
for a given specified length as shown below.import random
import string
def generateRandomAlphaNumericString(length):
# Generate alphanumeric string
letters = string.ascii_lowercase + string.digits
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random alphanumeric string of length", length, "is:", result_str)
generateRandomAlphaNumericString(12)
# Output
# Random alphanumeric string of length 12 is: ae8nd6149q7j
string.ascii_letters + string.digits + string.punctuation
and random.choice()
for a given specified length as shown below.import random
import string
def generateRandomStrongPassword(length):
# Generate random strong password
letters = string.ascii_letters + string.digits + string.punctuation
result_str = ''.join(random.choice(letters) for i in range(length))
print("Strong Password of length", length, "is:", result_str)
generateRandomStrongPassword(12)
# Output
# Strong Password of length 12 is: Au}h]D=aJ~QN
random.choice()
can repeat the characters. If you don’t want to repeat the characters, then use random.sample()
method. secrets
, and you could utilize this to generate a random secured password. The algorithm used by secrets is less predictable when compared to the random string module generation.import secrets
import string
def generateRandomCryptoPassword(length):
# Generate random password using cryptographic way
letters = string.ascii_lowercase + string.digits + string.punctuation
result_str = ''.join(secrets.choice(letters) for i in range(length))
print("Strong Password using Crpyto of length", length, "is:", result_str)
generateRandomCryptoPassword(12)
# Output
# Strong Password using Crpyto of length 12 is: ^6cg1(d^o/~(