python实现剪切功能

yipeiwu_com5年前Python基础

本文实例为大家分享了python实现剪切功能的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
#coding: utf8

import sys

mystr = []

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)uit
Please 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()

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python sqlobject(mysql)中文乱码解决方法

UnicodeEncodeError: 'latin-1' codec can't encode characters in position; 找了一天终于搞明白了,默认情况下,mys...

selenium+python自动化测试之多窗口切换

selenium+python自动化测试之多窗口切换

在很多页面上都有可点击的链接,点击这些链接会打开一个新的窗口,这时如果要在新打开的窗口中操作页面,就需要先切换到新窗口中,如果不进行切换操作,还是操作的上一个页面窗口 浏览器窗口的切换通...

PyCharm安装第三方库如Requests的图文教程

PyCharm安装第三方库如Requests的图文教程

PyCharm安装第三方库是十分方便的,无需pip或其他工具,平台就自带了这个功能而且操作十分简便。如下: 【注】:本人PyCharm已汉化,若是英文版按括号中英文指示操作即可。 1....

python3中dict(字典)的使用方法示例

一、clear(清空字典内容) stu = { 'num1':'Tom', 'num2':'Lucy', 'num3':'Sam', } print(stu.clear...

Python 2与Python 3版本和编码的对比

一、版本对比 首先要说的是,Python的版本,目前主要分为两大类: Python 2.x的版本的,被称为Python2:是目前用的最广泛的,比如Python 2.7.3。 Python...