How to generate a random string with symbols
Asked Answered
G

5

5

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.

Garceau answered 2/11, 2017 at 10:50 Comment(3)
string.ascii_letters + string.digits + '!@#$%^&*()_' add em to the mixSerg
what do u mean by symbols? Any character in unicode ? or just from ascii ?Champaign
I'm guessing string.printable isn't what you want because it includes newline, tab, etc?Phanerogam
P
10

How about:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation ) for n in range(12)])
Phanerogam answered 2/11, 2017 at 11:3 Comment(0)
I
2

Try:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits  ) for n in 
range(12)])
print(random)`
Insurmountable answered 18/2, 2019 at 9:10 Comment(0)
T
1

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

Timbale answered 18/2, 2019 at 9:23 Comment(0)
A
1

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.

Apocarpous answered 12/12, 2020 at 8:46 Comment(1)
this is a good pointNoaccount
P
1

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)
Pardoes answered 11/10, 2022 at 22:19 Comment(1)
use random.sample instead of loopsAilina

© 2022 - 2024 — McMap. All rights reserved.