【Python3】隨機字符串


# !/usr/bin/env python3
import random
import string
TEXT = string.punctuation + string.ascii_letters + string.digits
s1 = ''.join(random.sample(TEXT,k=32))
s3 = ''.join(random.choices(TEXT,k=32))

print('punctuation:', string.punctuation)
print('ascii_letters:', string.ascii_letters)
print('digits:', string.digits)
print(s1)
print(s3)
'''
punctuation: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
ascii_letters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
digits: 0123456789
)c^-,S`9OYyKmI\/:Xdx8;jD>n'E7VMf
q.j$<A!hCh[~Z|$60p?j`"3,YxZL0ovk
'''
import base64
import uuid

print('隨機uudi4:', uuid.uuid4())

ckst = base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes)
print('ckst:', ckst)

'''
隨機uudi4: 7de883e4-3086-44ea-82ef-b10fe4d4c985
ckst: b'8WYudFdQS+eZULOI5+3extP47xgsXEdPlwW6x8I5l2Q='
'''

參考:
Basic authentication on Tornado with a decorator https://gist.github.com/guillaumevincent/4771570
Python 生成一段隨機字符串的兩種寫法 https://www.v2ex.com/t/394944
Python 生成隨機字符串 https://www.jianshu.com/p/8c8763af7465


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM