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设计】。

相关文章

tornado+celery的简单使用详解

celery是实现一个简单,灵活可靠的分布式任务队列系统的好选择 tornado则不用过多介绍 在开发机上安装rabbitmq这里就不介绍了 首先是task文件的编写 task.py...

Python3.6通过自带的urllib通过get或post方法请求url的实例

废话不多说,直接上代码: # coding:utf-8 from urllib import request from urllib import parse url = "http...

使用Python Pandas处理亿级数据的方法

使用Python Pandas处理亿级数据的方法

在数据分析领域,最热门的莫过于Python和R语言,此前有一篇文章《别老扯什么Hadoop了,你的数据根本不够大》指出:只有在超过5TB数据量的规模下,Hadoop才是一个合理的技术选择...

Python 将pdf转成图片的方法

Python 将pdf转成图片的方法

本篇文章记录如何使用python将pdf文件切分成一张一张图片,包括环境配置、版本兼容问题。 环境配置(mac) 安装ImageMagick brew install imagemagi...

python安装pil库方法及代码

python安装pil库方法及代码

安装PIL 在Debian/Ubuntu Linux下直接通过apt安装: $ sudo apt-get install python-imaging Mac和其他版本的Linux...