background :
Different types of strings ( Upper and lower case letters , number , Special characters , chinese ), Put them together randomly , Different types of string lengths can be specified . as , All capital letters ;15 A number ; Upper and lower case letters , number , Special characters , Chinese 1 individual
environment :Windows7 + Python3.6.6
code :
import string import random class RandomStrings: def random_strings(self,
length_uper, length_lower, length_digit, length_spec, length_chn): uppers =
self._init_strings(string.ascii_uppercase) lowers =
self._init_strings(string.ascii_lowercase) digits =
self._init_strings(string.digits) specials = [chr(i) for i in range(33, 48)] +
[chr(i) for i in range(59, 65)] chns = [' in ', ' country '] results =
self._total_strings(uppers, length_uper) + self._total_strings(lowers,
length_lower) + self._total_strings(digits, length_digit) +
self._total_strings(specials, length_spec) + self._total_strings(chns,
length_chn) return ''.join(random.sample(result, len(result))) def
_init_strings(self, str): temp = [] for i in str: temp.append(i) return temp
def _total_strings(self, string_list, length): temp = [] for i in
range(length): temp.append(string_list[random.randint(0, len(string_list) -
1)]) return temp if __name__ == '__main__': rs = RandomStrings()
print(rs.random_strings(1, 1, 1, 1, 1))
remarks : Although the function is realized , There is no big one Bug. however , The code is not very good Python, Let's optimize it later ~~~
Technology