Python programming Quick Start Chapter 1 practical project reference answers, python Quick Start
1 #! /Usr/bin/env python3.5 2 # coding: UTF-8 3 import re 4 5 #7.18.1 6 7 # Strong Password detection 8 # Write a function using a regular expression, make sure that the entered password string is a strong password 9 # It must be at least 8 characters long and contain uppercase and lowercase letters. It must contain at least 1 digit, 10 11 pw = input ("enter the password :") 12 def checkpw (passwd): 13 plen = len (passwd) 14 print (plen) 15 chpw1 = re. compile (R '. * [A-Z] +. * ') 16 chpw2 = re. compile (R '. * [a-z] +. * ') 17 chpw3 = re. compile (R '. * \ d {1 ,}. * ') 18 chresult1 = chpw1.search (passwd) 19 print ("matching uppercase characters", chresult1) 20 chres Ult2 = chpw2.search (passwd) 21 print ("matching lowercase characters", chresult2) 22 chresult3 = chpw3.search (passwd) 23 print ("matching at least 1 digit", chresult3) 24 if (plen> = 8) and (chresult1! = None) and (chresult2! = None) and (chresult3! = None): 25 print ("Your Password meets requirements") 26 else: 27 print ("your password does not meet requirements") 28 29 checkpw (pw) 30 31 # 7.18.htm # Write a function that accepts a string and does the same thing as strip (). 33 # If only the string to be removed is input, there is no other parameter, otherwise, the character specified by the second parameter of the function removes 35 36 # From the character and passes two parameters: str1 will be removed from the string, str2 accepts the original string given by the user 37 # Note: str1 has a default value, pay attention to its location. 38 39 string = input ("please give a raw string to be processed:") 40 repstr = input ("Please enter a string to be deleted:") 41 def newstrip (str2, str1 = ''): 42 # define the x and y variables to be passed to the regular expression. x is used to match the blank characters starting with the original string, y is used to match the blank character 43 x = '^ \ s * '44 y =' \ s * $'45 at the end of the original string. # If the user does not enter the string to be deleted, returns the original string that removes the leading and trailing spaces; otherwise, returns the new string 46 if str1 = '': 47 newstr = re. sub (r '% s | % s' % (x, y), '', str2) 48 print (" You have not entered any characters to be removed, by default, ") 49 else: 50 newstr = re. sub (str1, '', str2) 51 print (" character "+ str1 +" will be removed from the original string ") 52 return newstr53 print (" the processed string is: ") 54 if repstr in string: 55 print (newstrip (string, repstr) 56 else: 57 print (" your input string is not in the original string, or is not continuous ")