Question 1th string substitution
Push the letters in the string backwards by two, for example A to C,z to B and the other characters unchanged.
1 defconvert_string (astring):2bstring =' '3 forIinchastring:4 ifOrd'a') <=ord (i) <=ord ('W')orOrd'A') <=ord (i) <=ord ('W'):5k = Chr (ord (i) +2)6 elifi = ='y' ori = ='Z':7k = Chr (Ord ('a') +1+ord (i)-ord ('Z'))8 elifi = ='Y' ori = ='Z':9k = Chr (Ord ('A') +1+ord (i)-ord ('Z'))Ten Else: OneK =I ABstring + =k - returnBstring
Effect:
Official recommended method for Maketrans
" abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz " "cdefghijklmnopqrstuvwxyzabcdefgfijklmnopqrstuvwxyzab"= Thestring.translate (Thestring.maketrans (intab,outtab))
Maketrans returns the conversion list, which converts the character in the string Intab to the character in the corresponding position in the Outtab, and translate performs the conversion operation on the converted list returned by Maketrans.
Question 2nd finding characters from a string
View the Web page source code and save that heap of characters in a file rare_char.txt text file.
File = open ("/home/zhanchao/desktop/rare_char.txt"= file.read () for in filestr: if"a"<=i<=" z": print(i)
Python Challenge Second question string substitution