如何在Python中生成包含大寫字母和數字的隨機字串?


您可以使用Python生成包含大寫字母和數字的隨機字串。這裡有三種不同的方法。

使用random.choices()和string.ascii_uppercase

在這個方法中,我們將使用random.choices()函式從string.ascii_uppercase和string.digits字串中隨機選擇字元來生成隨機字串。

示例

我們首先匯入random和string模組,我們將分別使用它們來生成隨機字元和建立字串。

我們定義要生成的隨機字串的長度(在這個例子中是10)。

我們定義可以用來建立隨機字串的可能的字元,它是string.ascii_uppercase(所有大寫字母)和string.digits(所有數字)的組合。

我們使用random.choices()函式從characters字串中隨機選擇k個(等於隨機字串的長度)字元。

然後,我們使用.join()方法將這些字元連線成單個字串。

最後,我們列印生成的隨機字串。

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits

# use random.choices() to generate the random string
result = ''.join(random.choices(characters, k=length))
# print the result
print(result)

輸出

ZMVICXPMKQ

使用random.choice()和迴圈

在這個方法中,我們將使用for迴圈和random.choice()函式來生成隨機字元並將它們新增到字串中,直到達到所需的長度。

示例

我們首先匯入random和string模組,我們將分別使用它們來生成隨機字元和建立字串。

我們定義要生成的隨機字串的長度(在這個例子中是10)。

我們定義可以用來建立隨機字串的可能的字元,它是string.ascii_uppercase(所有大寫字母)和string.digits(所有數字)的組合。

我們定義一個名為result的空字串變數來儲存生成的隨機字元。

我們使用for迴圈使用random.choice()函式生成一個新的隨機字元,並在每次迴圈迭代中將其新增到result字串中,直到我們獲得所需長度的隨機字串。

最後,我們列印生成的隨機字串。

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits

# define an empty string to hold the random characters
result = ''
# use a loop to generate the random string
for i in range(length):
    result += random.choice(characters)
# print the result
print(result)

輸出

Y9V1OVO4H4

使用uuid.uuid4()和str.replace()

在這個方法中,我們將使用uuid.uuid4()函式生成一個隨機UUID(通用唯一識別符號),然後使用str.replace()方法刪除連字元並將字串轉換為全大寫字母。

示例

我們匯入uuid模組,我們將使用它來生成隨機UUID。

我們使用uuid.uuid4()函式生成一個隨機UUID,並使用str()方法將其轉換為字串。

我們使用.replace()方法刪除UUID字串中的連字元,並用空字串替換它們。

import uuid
# generate a random UUID
random_uuid = str(uuid.uuid4())
# remove the hyphens and convert to uppercase letters
result = random_uuid.replace('-', '').upper()
# print the result
print(result)

輸出

3D9FF7622A494CABA861D0EE62547BED

使用random.sample()和string.ascii_uppercase

在這個方法中,我們將使用random.sample()函式從string.ascii_uppercase和string.digits字串中隨機選擇k個字元(其中k是所需隨機字串的長度)。

示例

我們首先匯入random和string模組,我們將分別使用它們來生成隨機字元和建立字串。

我們定義要生成的隨機字串的長度(在這個例子中是10)。

我們定義可以用來建立隨機字串的可能的字元,它是string.ascii_uppercase(所有大寫字母)和string.digits(所有數字)的組合。

我們使用random.sample()函式從characters字串中隨機選擇k個(等於隨機字串的長度)字元。

然後,我們使用.join()方法將這些字元連線成單個字串。

最後,我們列印生成的隨機字串。

import random
import string
# define the length of the random string
length = 10
# define the possible characters that can be used
characters = string.ascii_uppercase + string.digits
# use random.sample() to generate the random string
result = ''.join(random.sample(characters, k=length))
# print the result
print(result)

輸出

VJMFQO43XL

使用random.randint()和chr()

在這個方法中,我們將使用random.randint()函式生成'0'和'Z'的ASCII碼之間的隨機整數,然後使用chr()函式將這些整數轉換為它們對應的ASCII字元。

示例

我們匯入random模組,我們將使用它來生成隨機整數和字元。

我們定義要生成的隨機字串的長度(在這個例子中是10)。

我們使用random.randint()函式生成'0'(即48)和'Z'(即90)的ASCII碼之間的隨機整數,然後使用chr()函式將其轉換為其對應的ASCII字元。

我們使用for迴圈重複此過程以獲得所需長度的隨機字串,並將這些字元使用.join()方法連線成單個字串。

最後,我們列印生成的隨機字串。

import random
# define the length of the random string
length = 10
# use random.randint() and chr() to generate the random string
result = ''.join(chr(random.randint(48, 90)) for _ in range(length))
# print the result
print(result)

輸出

W0FW?RL2?Z

這些是一些在Python中生成包含大寫字母和數字的隨機字串的示例。您可以選擇最符合您的需求和偏好的方法。

更新於:2023年8月10日

936 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.