Python 程序:生成随机字符串

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

**

使用 random.choice()

python 字符串中使用了 random.choice() 函数来生成可以以任何顺序重复该字符串的字符和数字序列。

使用 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

输出:

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

| 方法 | 描述 |
| 字符串. ascii _ 字母 | 它返回一个随机字符串,包含大写和小写字符。 |
| string ascii upper case | 这是一个随机字符串方法,只返回大写字符的字符串。 |
| string . ascii _ 小写 | 这是一个随机字符串方法,只返回小写字符的字符串。 |
| 字符串数字 | 这是一个随机字符串方法,返回带有数字字符的字符串。 |
| 字符串.标点符号 | 这是一个随机字符串方法,返回带有标点符号的字符串。 |

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

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

输出:

指定字符的随机字符串

special . 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)

输出:

注意:python 程序中使用了 random.choice()方法来重复相同的字符串。如果我们不想显示重复的字符,我们应该使用 random.sample()函数。

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

撤回重复。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) 

输出:

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

生成由固定字母和数字组成的随机字母数字字符串

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

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

固定环. 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))

输出:

使用秘密。选择()

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))

输出:

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

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

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))

输出:


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/236447.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:00
下一篇 2024-12-12 12:00

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29

发表回复

登录后才能评论