python 随机生成字符串

来源:互联网 发布:手机淘宝6.5.0旧版本 编辑:程序博客网 时间:2024/05/01 16:27
  1. import random
  2. importstring
  3. import os
  4. #第一种方法
  5. seed ="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
  6. sa =[]
  7. for iin range(8):
  8. sa.append(random.choice(seed))
  9. salt =''.join(sa)
  10. print salt

  1. #第二种方法
  2. salt =''.join(random.sample(string.ascii_letters+string.digits,8))
  3. print salt

#第三种方法(不适用与python3.x)
temp = ''.join(map(lambda xx:(hex(ord(xx))[2:]),os.urandom(3)))
print temp

0 0
原创粉丝点击