本文實例為大家分享了python實現剪切功能的具體代碼,供大家參考,具體內容如下
#!/usr/bin/env python#coding: utf8import sysmystr = []def inputstr(): item = raw_input('Please input your string:') mystr[:] = [] #清空列表 mystr.extend(item) #將輸入的字符串拆開為一個一個字符填入列表def printstr(): lenth = len(mystr) - 1 index = 0 print "Your result is :" print "*****" + ''.join(mystr) + "*****" #.join()與之前的extend對應,將字符合并為一個元素,用''里面的內容分割。''里面為空,則字符之間沒有間隙 print "----------------分割符----------------"def leftstrip(): #左剪切 while True: if mystr[0] == ' ': mystr.pop(0) else: break printstr()def rightstrip():#右剪切 while True: if mystr[-1] == ' ': mystr.pop() else: break printstr()def bothsidestrip(): while True: if mystr[-1] == ' ': mystr.pop() elif mystr[0] == ' ': mystr.pop(0) else: break printstr()#使用字典的方式,實現case的語法功能CMDs = {'l':leftstrip,'r':rightstrip,'b':bothsidestrip}def showmenu(): prompt = """(L)eftstrip(R)ightstrip(B)othsidestrip(Q)uitPlease select a choice:""" while True: choice = raw_input(prompt).lower() if choice not in 'lrbq': continue if choice == 'q': break inputstr() CMDs[choice]()if __name__=='__main__': showmenu()效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答