python生成随机字符串_Python生成随机字符串
2021/10/17 14:39:29
本文主要是介绍python生成随机字符串_Python生成随机字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python生成随机字符串
We can generate a random string in Python using the random module. Sometimes we want to generate a random string for unique identifiers, session id or to suggest a password.
我们可以使用random模块在Python中生成随机字符串。 有时我们想为唯一标识符,会话ID或建议密码生成一个随机字符串。
Python生成随机字符串 (Python Generate Random String)
Let’s define the utility function to generate a random string from the given sequence of characters and specified size.
让我们定义实用程序函数,以根据给定的字符序列和指定的大小生成随机字符串。
import random import string def random_string_generator(str_size, allowed_chars): return ''.join(random.choice(allowed_chars) for x in range(str_size)) chars = string.ascii_letters + string.punctuation size = 12 print(chars) print('Random String of length 12 =', random_string_generator(size, chars))
Output:
输出:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;?@[\]^_`{|}~ Random String of length 12 = 'MP'?EI$MO%w
We are using random.choice()
function to select a random character from the provided sequence of characters. Then we are using for loop to run it given number of times. Then we are using string join() function to concatenate them and return the randomly generated string.
我们正在使用random.choice()
函数从提供的字符序列中选择一个随机字符。 然后,我们使用for循环运行给定次数。 然后,我们使用字符串join()函数将它们连接起来并返回随机生成的字符串。
What if we want to keep the random string size as variable, say between 8 and 12 characters. Let’s tweak our function little bit to randomly select the size of the random string.
如果我们想将随机字符串的大小保留为变量,例如8到12个字符,该怎么办? 让我们稍微调整一下函数以随机选择随机字符串的大小。
import random from random import randint import string def random_string_generator_variable_size(min_size, max_size, allowed_chars): return ''.join(random.choice(allowed_chars) for x in range(randint(min_size, max_size))) chars = string.ascii_letters + string.punctuation print('Random String of random length (6-12) =', random_string_generator_variable_size(6, 12, chars))
Output: Random String of random length (6-12) = d;@o/?[yq=
输出: Random String of random length (6-12) = d;@o/?[yq=
The code is almost the same as earlier function except for the use of the randint()
function. This is done to randomly select the size of the randomly generated string.
除了使用randint()
函数外,该代码与早期函数几乎相同。 这样做是为了随机选择随机生成的字符串的大小。
随机UUID生成 (Random UUID Generation)
If you want a Unique ID based on RFC-4122 specifications, then you can use Python uuid module.
如果您想要基于RFC-4122规范的唯一ID,则可以使用Python uuid模块。
import uuid print('Random UUID from uuid1() =', uuid.uuid1()) print('Random UUID from uuid4() =', uuid.uuid4())
Output:
输出:
Random UUID from uuid1() = dcc1044e-d76b-11e8-b54e-186590db0e15 Random UUID from uuid4() = a0e98c8d-a6fd-4125-bc1c-69ffe6456cb6
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。
翻译自: https://www.journaldev.com/23782/python-generate-random-string
这篇关于python生成随机字符串_Python生成随机字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-02Python编程基础
- 2024-11-01Python 基础教程
- 2024-11-01用Python探索可解与不可解方程的问题
- 2024-11-01Python编程入门指南
- 2024-11-01Python编程基础知识
- 2024-11-01Python编程基础
- 2024-10-31Python基础入门:理解变量与数据类型
- 2024-10-30Python股票自动化交易资料详解与实战指南
- 2024-10-30Python入行:新手必读的Python编程入门指南
- 2024-10-30Python入行:初学者必备的编程指南