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