Python method analysis for generating 8 bit random strings

  • 2020-06-15 09:20:15
  • OfStack

This article is an example of how Python generates 8-bit random strings. To share for your reference, specific as follows:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import string
# The first 1 methods 
seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
sa = []
for i in range(8):
  sa.append(random.choice(seed))
salt = ''.join(sa)
print salt
# Operation results: l7VSbNEG
# The first 2 methods 
salt = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print salt
# Operation results: VOuCtHZs

Generate random strings

A good way to encrypt a user's password is to generate a random string and then mix it with the password to extract the summary. The method that produces the random string finds this.

The first is simpler and easier to understand

The second one is hard to understand, but it's pretty neat

random.randint(1000,9999). But it doesn't start with a zero, which is a little annoying, and then I find this article. Long to see

PS: Here are a few more tools for your reference:

Online random generation of personal information data tools:
http://tools.ofstack.com/aideddesign/rnd_userinfo

Online random character/random password generation tool:
http://tools.ofstack.com/aideddesign/rnd_password

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

More about Python related content interested readers to view this site project: "Python mathematical operation skills summary", "Python string skills summary", "Python coding skills summary", "Python data structure and algorithm tutorial", "Python function using skills summary", "introduction to Python and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful in Python programming.


Related articles: