Python Challenge Second question string substitution

Source: Internet
Author: User
Tags ord

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.