Recents in Beach

Strong password generate

About Blog Best learn Engg. Subject like DAA,Operating System,C language, Python ,Daily Coding Challenges ..this blog Provides Tutorials And coding Challenges of C,Python, OS,DAA,Algorithms and other...for all Beginners...
It is Great way to improve Your skills when learning to Code and solving code challenges,Solving Different Type of Challenges and Puzzles....

The best way to learn Python is by practicing examples. The page contains examples on basic concepts of Python. You are advised to take the references from these examples and try them on your own.


1 ) Generate a random string of a fixed length.

    Use the string constant string.ascii_lowercase to get all the lowercase letters. I.e., 'abcdefghijklmnopqrstuvwxyz'


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import random
import string

def randomString(stringLength=8):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))

print("Random String is ", randomString())
print("Random String is ", randomString(8))
print("Random String is ", randomString(8))

Output :



2 ) Generate a random alphanumeric string with letters and numbers.
    
   Many times we want to create a random string that contains both letters and digit. For example, you want a random string.

 like ab23cd, jkml98, 87thki.

           We can use the string.ascii_letters  and string.digits  constants to get the combinations of letters and digits in our random string.
Now, let’s see the example to generate a random string with letters and digits in Python. In this example, we are creating a random string with the combination of a letter from A-Z, a-z, and digits 0-9.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import random
import string

def get_random_alphaNumeric_string(stringLength=8):
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join((random.choice(lettersAndDigits) for i in range(stringLength)))

print("First alphaNumeric Random String is  ", get_random_alphaNumeric_string(8))
print("Second alphaNumeric Random String is ", get_random_alphaNumeric_string(8))
print("Third alphaNumeric Random String is  ", get_random_alphaNumeric_string(8))


Output :



3 ) Generate a random password string with Special characters, letters, and digits

The password which contains characters, digits and special symbols are considered to be a strong password.
For example, you want to generate a random Password like this.
  • ab23cd#$
  • jk%m&l98
  • 87t@h*ki

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import random
import string

def randomString(stringLength=10):
    password_characters = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(password_characters) for i in range(stringLength))

print("Generating Random String password with letters, digits and special characters ")
print ("First Random String ", randomString() )
print ("Second Random String", randomString(10) )
print ("Third Random String", randomString(10) )

Output :




Post a Comment

0 Comments