随机指的是数据或信息的集合,可以以任何顺序出现。Python中的random模块用于生成随机字符串。随机字符串由数字、字符和标点符号序列组成,可以包含任何模式。random模块包含两个方法random.choice()secrets.choice(),用于生成安全字符串。让我们了解如何使用random.choice()和secrets.choice()方法在Python中生成随机字符串。

139-1.png

使用random.choice()

random.choice()函数用于在Python字符串中生成可以按任何顺序重复字符串的字符和数字序列。

创建一个使用random.choices()函数生成随机字符串的程序。

Random_str.py

import string    
import random # define the random module  
S = 10  # number of characters in the string.  
# call random.choices() string module to find the string in Uppercase + numeric data.  
ran = ''.join(random.choices(string.ascii_uppercase + string.digits, k = S))    
print("The randomly generated string is : " + str(ran)) # print the random data  

输出:

139-2.png

以下是用于生成随机字符串的random模块中使用的方法。

方法描述
String.ascii_letters返回一个包含大写和小写字符的随机字符串。
String_ascii_uppercase这是一个仅返回大写字符的随机字符串方法。
String.ascii_lowercase这是一个仅返回小写字符的随机字符串方法。
String.digits这是一个返回带有数字字符的随机字符串方法。
String.punctuation这是一个返回带有标点符号字符的随机字符串方法。

生成包含大写字母和小写字母的随机字符串

UprLwr.py

# write a program to generate the random string in upper and lower case letters.  
import random  
import string  
def Upper_Lower_string(length): # define the function and pass the length as argument  
    # Print the string in Lowercase  
    result = ''.join((random.choice(string.ascii_lowercase) for x in range(length))) # run loop until the define length  
    print(" Random string generated in Lowercase: ", result)  
  
    # Print the string in Uppercase  
    result1 = ''.join((random.choice(string.ascii_uppercase) for x in range(length))) # run the loop until the define length  
    print(" Random string generated in Uppercase: ", result1)  
  
Upper_Lower_string(10) # define the length  

输出:

139-3.png

特定字符的随机字符串

Specific.py

# create a program to generate the random string of given letters.  
import random  
import string  
def specific_string(length):  
    sample_string = 'pqrstuvwxy' # define the specific string  
    # define the condition for random string  
    result = ''.join((random.choice(sample_string)) for x in range(length))  
    print(" Randomly generated string is: ", result)  
  
specific_string(8) # define the length  
specific_string(10)  

输出:

139-4.png

注意:在Python程序中,random.choice()方法用于重复相同字符字符串。如果我们不希望显示重复的字符,应使用random.sample()函数。

生成不重复相同字符的随机字符串

WithoutRepeat.py

# create a program to generate a string with or without repeating the characters.  
import random  
import string  
print("Use of random.choice() method")  
def specific_string(length):  
     
    letters = string.ascii_lowercase # define the lower case string  
     # define the condition for random.choice() method  
    result = ''.join((random.choice(letters)) for x in range(length))  
    print(" Random generated string with repetition: ", result)  
  
specific_string(8) # define the length  
specific_string(10)  
  
  
print("") # print the space  
print("Use of random.sample() method")  
def WithoutRepeat(length):  
    letters = string.ascii_lowercase # define the specific string  
    # define the condition for random.sample() method  
    result1 = ''.join((random.sample(letters, length)))   
    print(" Random generated string without repetition: ", result1)  
  
WithoutRepeat(8) # define the length  
WithoutRepeat(10)   

输出:

139-5.png

如上所示,在上面的输出中,random.sample()方法返回一个所有字符都是唯一且不重复的字符串。而random.choice()方法返回一个可能包含重复字符的字符串。因此,我们可以说如果要生成唯一的随机字符串,应使用random.sample()方法。

生成包含固定字母和数字的随机字母数字字符串

例如,假设我们希望生成一个随机生成的包含五个字母和四个数字的字母数字字符串。我们需要将这些参数定义到函数中。

让我们编写一个程序,生成一个包含固定数量字母和数字的字母数字字符串。

fixedString.py

import random  
import string  
def random_string(letter_count, digit_count):  
    str1 = ''.join((random.choice(string.ascii_letters) for x in range(letter_count)))  
    str1 += ''.join((random.choice(string.digits) for x in range(digit_count)))  
  
    sam_list = list(str1) # it converts the string to list.  
    random.shuffle(sam_list) # It uses a random.shuffle() function to shuffle the string.  
    final_string = ''.join(sam_list)  
    return final_string  
  
# define the length of the letter is eight and digits is four  
print("Generated random string of first string is:", random_string(8, 4))  
  
# define the length of the letter is seven and digits is five  
print("Generated random string of second string is:", random_string(7, 5))  

输出:

139-6.png

使用secrets.choice()

secrets.choice()方法用于生成比random.choice()更安全的随机字符串。它是一个加密随机字符串生成器,使用secrets.choice()方法可以确保没有两个进程可以同时获取相同的结果。

让我们编写一个程序,使用secrets.choice方法打印安全的随机字符串。

Secret_str.py

import random   
import string  
import secrets # import package  
num = 10 # define the length of the string  
# define the secrets.choice() method and pass the string.ascii_letters + string.digits as an parameters.  
res = ''.join(secrets.choice(string.ascii_letters + string.digits) for x in range(num))  
  
# print the Secure string   
print("Secure random string is :"+ str(res))  

输出:

139-7.png

使用random模块的不同方法生成安全的随机字符串。

让我们编写一个程序,使用secrets.choice()的不同方法打印安全的随机字符串。

Secret.py

# write a program to display the different random string method using the secrets.choice().  
# imports necessary packages  
import random   
import string  
import secrets  
num = 10 # define the length of the string  
# define the secrets.choice() method and pass the string.ascii_letters + string.digits as an parameters.  
res = ''.join(secrets.choice(string.ascii_letters + string.digits) for x in range(num))  
# Print the Secure string with the combination of ascii letters and digits  
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.ascii_letters) for x in range(num))  
# Print the Secure string with the combination of ascii letters   
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.ascii_uppercase) for x in range(num))  
# Print the Secure string in Uppercase  
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.ascii_lowercase) for x in range(num))  
# Print the Secure string in Lowercase  
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.ascii_letters + string.punctuation) for x in range(num))  
# Print the Secure string with the combination of letters and punctuation  
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.digits) for x in range(num))  
# Print the Secure string using string.digits  
print("Secure random string is :"+ str(res))  
  
res = ''.join(secrets.choice(string.ascii_letters + string.digits + string.punctuation) for x in range(num))  
# Print the Secure string with the combonation of letters, digits and punctuation   
print("Secure random string is :"+ str(res))  

输出:

139-8.png

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程