Python Core programming 2 The sixth chapter after class practice

Source: Internet
Author: User

6-1 string. Is there a string method or function in the string module that can help me identify if a string is part of another large string?

#!/usr/bin/env pythondef contain (STR1,STR2):        if str1 in str2:                print "str1 contains str2"        else:                print " STR1 not contains str2 "if __name__==" __main__ ":        str1=raw_input (" Please type the string1: ")        str2=raw_input (" Please type the string2: ")        contain (STR1,STR2)

6-2 string identifier. Modify the idcheck.py script in example 6-1 so that it can detect an identifier of length one, and can recognize the Python keyword, for the latter, you can use the keyword module (especially keyword.kelist) to help you.

#!/usr/bin/env pythonimport stringimport keywordalphas=string.letters+ ' _ ' nums=string.digitskeywords= Keyword.kwlistprint ' Welcome to the Identifier Checker v1.0 ' print ' testees must is at least 2 chars long. ' Myinput=raw_input (' Identifier to test? ') If myinput in keywords:                print "%s can is not is the keyword"%myinputelif len (myinput) >1:        if myinput in keywords:
   print "Chars can not contain the keywords"        if myinput[0] not in Alphas:                print "Invalid:first symbol must is a Lphabetic "        else: for                Otherchar in myinput[1:]:                        if Otherchar not in Alphas +nums:                                print" ' Invalid: Remaining symbols must is alphanumeric "break                Else:                        print" Okay as an identifier "else:        print" the Word is illegal identifier for Python "
6-3 sort

(a) Enter a series of numbers, ranging from large to small.

#!/usr/bin/env python#-*-coding:utf-8-*-string=raw_input ("Please enter the string:") print sorted (string)

(b) Same as a, but with a dictionary order from large to small.

#!/usr/bin/env pythonstring =raw_input ("Please enter the number:") List =string.split () print listprint sorted (list)

  

Python Core programming 2 The sixth chapter after class practice

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.