Small Q: The atrium of the white tree-dwelling crow, cold dew silent wet osmanthus; Tonight the Ming Dynasty, I do not know the autumn think of who home? Wang Jian "The Moon in the Night"
Two password generation code instances
=============================================================
#!/usr/bin/python#-*-Coding:utf8-*-from Random import randrange, samplepassword_list = [' Z ', ' y ', ' x ', ' w ', ' V ', ' u ', ' t ', ' s ', ' r ', ' Q ', ' P ', ' O ', ' n ', ' m ', ' l ', ' K ', ' j ', ' I ', ' h ', ' G ', ' f ', ' e ', ' d ', ' C ', ' B ', ' A ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', '! ', ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', ' _ ', ' + ', '-', ' = ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ']leng=15password = ' ". Join (sample (Password_list, Leng) ). Replace (",") print (password)
Import ... #导入模块里所有属性或方法.
From......import #针对性的导入, which means importing the properties or methods you want to import into the module.
Import OS #导入os模块里所有的属性和方法
The from random import Randint #从random模块里仅仅导入 the Randint method.
Python Replace () method
Str.replace (old, new[, Max])
#!/usr/bin/python
str = "This is string EXAMPLE....WOW!!! This is really string ";
Print Str.replace ("is", "was");
Print Str.replace ("is", "was", 3);
>>>>>>>>
Thwas was string Example....wow!!! Thwas was really string
Thwas was string Example....wow!!! Thwas is really string
Join () function
' Sep '. Join (SEQ)
Parameter description
Sep: Delimiter. Can be empty
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter
Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep
######## #对序列进行操作 (use ' and ': ' as separators, respectively)
>>> seq1 = [' Hello ', ' good ', ' boy ', ' Doiido ']
>>> print '. Join (SEQ1)
Hello Good boy Doiido
>>> print ': ' Join (SEQ1)
Hello:good:boy:doiido
######## #对字符串进行操作
>>> seq2 = "Hello Good boy Doiido"
>>> print ': ' Join (SEQ2)
H:e:l:l:o:: g:o:o:d:: b:o:y::d: O:i:i:d:o
######### #对元组进行操作
>>> seq3 = (' Hello ', ' good ', ' boy ', ' Doiido ')
>>> print ': ' Join (SEQ3)
Hello:good:boy:doiido
######### #对字典进行操作
>>> seq4 = {' Hello ': 1, ' good ': 2, ' Boy ': 3, ' Doiido ': 4}
>>> print ': ' Join (SEQ4)
Boy:good:doiido:hello
Os.path.join () function
Syntax: Os.path.join (path1[,path2[,......])
Return value: Combine multiple paths to return
Note: Parameters before the first absolute path are ignored
############ #合并目录
>>> Import OS
>>> os.path.join ('/hello/', ' good/boy/', ' Doiido ')
'/hello/good/boy/doiido '
Random module Important functions
1) and random () returns the random real number n between 0<=n<1;
2), choice (seq) returns a random element from the sequence seq;
3), Getrandbits (n) returns n random bits in the form of a long integer;
4), Shuffle (seq[, random]) specify the SEQ sequence in situ;
5), sample (SEQ, n) select n Random and independent elements from the sequence seq;
=============================================================
Import String,randomlength=8seedlower=string.lowercaseseeddigit=string.digitsseedupper=string.uppercasepwd=pwdd =pwdl=pwdu= ' Countl=random.randrange (1,length-1) countu=random.randrange (1,length-countl) countd= ( Length-countl-countu) #生成随机的字符for L in Random.sample (seedlower,countl):p wdl+=lfor u in Random.sample (Seedupper,countu ):p wdu+=ufor D in Random.sample (SEEDDIGIT,COUNTD):p wdd+=d# random character seed=pwdl+pwdu+pwddshuffler=random.sample at random locations ( Seed,len (seed)) pwd= "". Join (shuffler) print pwd
Constants in the string module:
String.digits: Digital 0~9
String.letters: All Letters (case)
String.lowercase: All lowercase letters
String.printable: A string that can print characters
String.punctuation: All punctuation
String.uppercase: All uppercase letters
Python--Password generator