computing245
computing245

Reputation: 51

How to generate a random string with symbols

My code:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits ) for n in range(12)])

So far it prints a string which contains uppercase, lowercase letters and numbers, but I don't know how to make it print symbols as well.

Upvotes: 5

Views: 31383

Answers (5)

Jim McNeely
Jim McNeely

Reputation: 817

I'm sure there's a better way to do this, but you can weight them to have more letters and numbers and fewer punctuation characters like this:

import random
import string

pw = ''.join([random.choice(string.ascii_letters + string.ascii_letters + string.ascii_letters + string.digits + string.digits + string.digits + string.punctuation ) for n in range(12)])
print(pw)

Upvotes: 1

Bogdan Neterebskii
Bogdan Neterebskii

Reputation: 21

Slightly another variant:

import random
import string
random_string = ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation ) for n in range(12)])

In this case you do not redefine random form library.

Upvotes: 1

Mayur Kachare
Mayur Kachare

Reputation: 21

Try:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits  ) for n in 
range(12)])
print(random)`

Upvotes: 2

Deepak
Deepak

Reputation: 591

To generate a random string we need to use the following two Python modules.

  • String module which contains various string constant which contains the ASCII characters of all cases. String module contains separate constants for lowercase, uppercase letters, digits, and special characters.

  • random module to perform the random generations.

    Let see steps to generate a random string of a fixed length of n.

  • Use the string constant string.ascii_lowercase to get all the lowercase letter in a single string.

  • The string.ascii_lowercase constant contains all lowercase letters. I.e., 'abcdefghijklmnopqrstuvwxyz' Run for loop n number of times to pick a single character from a string constant using a random.choice method and add it to the string variable using a join method. The choice method used to choose the single character from a list

  • For example, Suppose you want a random string of length 6 then we can execute a random.choice() method 6 times to pick a single letter from the string.ascii_lowercase and add it to the string variable.

Let see the code now.

import random
import string

def randomStringwithDigitsAndSymbols(stringLength=10):
    """Generate a random string of letters, digits and special characters """
    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 ", randomStringwithDigitsAndSymbols() )
print ("Second Random String", randomStringwithDigitsAndSymbols(10) )
print ("Third Random String", randomStringwithDigitsAndSymbols(10) )

Output:

Generating Random String password with letters, digits and special characters 
First Random String password  qKDhC++T(4
Second Random String password  U+(ew5a[#U
Third Random String password  uf-g,s6'pX

Use this link for more details Click here

Upvotes: 1

Luke Smith
Luke Smith

Reputation: 967

How about:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation ) for n in range(12)])

Upvotes: 10

Related Questions