6-1. Is there a string method or function in the string. String module that can help me identify whether a character is part of another large string?
#You can use dir(sting) to view all methods and properties s='sbcdefg' s.find('cde') s.find('cde',0) s.find('cde',0,len(s))
6-2. String identifier. Modify the idcheck.py Script, so that it can detect identifier with length of one, and recognize Python keyword. For the latter requirement, you can use keyword module (especially keyword.kelist) to help you.
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*- """ idcheck.py Determine the validity of filename1.Start with underscore 2.One character can be used as identifier 3.Cannot be a keyword""" import keyword as kw import string import sys def idcheck(s): startwith=string.letters+'_' othersymbol=string.letters+'_'+string.digits if len(s)>0: if len(s)==1: print 'Enter as 1 character length'.decode('utf-8').encode('gbk') if s in startwith: print 'Naming rules for single character symbols'.decode('utf-8').encode('gbk') else: print 'Single character does not conform to naming rules'.decode('utf-8').encode('gbk') sys.exit() else: if s in kw.kwlist: print 'The character you entered is a keyword. Please re-enter'.decode('utf-8').encode('gbk') else: if s not in startwith: print 'The first character does not conform to the naming rules'.decode('utf-8').encode('gbk') sys.exit() else: for i in s[1:]: if i not in othersymbol: print 'Other characters are not legal identifiers, please re-enter'.decode('utf-8').encode('gbk') else: print 'Other characters are legal identifiers'.decode('utf-8').encode('gbk') else: print 'Please enter a keyword with a length greater than or equal to 1 character'.decode('utf-8').encode('gbk') if __name__=='__main__': s=raw_input('Enter a file name:').strip() idcheck(s)
(a) enter a string of numbers, arranged from large to small.
(b) same as a, but in lexicographic order from large to small.
# -*- coding:utf-8 -*- """ sort""" import keyword as kw import string import sys def f_sort(s): elist=list(s) elist.sort() print elist s=raw_input('Enter row number:') f_sort(s)
9. conversion. Write a sister function for exercise 5-13 that takes minutes and returns hours and minutes. The total time is constant and the number of hours is required to be as large as possible. .
# -*- coding:utf-8 -*- """ Enter minutes,Convert to hours and minutes""" def f_swaptime(s): num=int(s) hour,min=num/60,num%60 print '%d hours:%d mins'%(hour,min) s=raw_input('Enter row number:') f_swaptime(s)
6-10 string. Write a function that returns a string similar to the input string, requiring the string to be flipped in case. For example, if you enter "mr.ed", you should return "mr.ed" as the output.
# -*- coding:utf-8 -*- """ swapcase()""" def f_swapcase(s): s=s.swapcase() print s s=raw_input('Enter row number:') f_swapcase(s)
(a) create an integer to IP address converter
(b) update your program content so that it can be reversed
# -*- coding:utf-8 -*- """ ip Conversion between and integers""" def convert_ip(num): #bin starts with 0b num_bin=bin(int(num))[2:] str_bin=str(num_bin).zfill(32) b1=str_bin[0:8] b2=str_bin[8:16] b3=str_bin[16:24] b4=str_bin[24:32] b_out=b4+'.'+b3+'.'+b2+'.'+b1 print b_out return b_out num=raw_input('Enter row number:') s=convert_ip(num)
[reference article] https://www.cnblogs.com/tmyyss/p/3758134.html