方法一、依賴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
方法二、利用自己輸出的字符串
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import string #第一種方法 seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-" sa = [] for i in range(8): sa.append(random.choice(seed)) salt = ''.join(sa) print salt #第二種方法 salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) print salt
GitHub Repository. GitHub存儲庫中檢出完整的python腳本和更多Python示例。
翻譯自: https://www.journaldev.com/23782/python-generate-random-string