0%

python生成随机字符串

方法

1.大小写字母

1
2
3
>>> import random, string
>>> "".join(random.sample(string.ascii_letters,9))
'HkJdpZclC'

2.大小字母+数字(字母开头)

1
2
3
>>> import random, string
>>> "".join(random.sample(string.ascii_letters,1)+random.sample(string.ascii_letters+string.digits,9))
'CX41uNHykR'

元素拆解

1
2
3
4
5
6
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> random.sample('123456789', 4)
['7', '8', '5', '4']