python实现剪切功能

yipeiwu_com6年前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正则表达式re模块

正则是处理字符串最常用的方法,我们编码中到处可见正则的身影。 正则大同小异,python 中的正则跟其他语言相比略有差异: 1、替换字符串时,替换的字符串可以是一个函数 2、split...

基于Python 的进程管理工具supervisor使用指南

基于Python 的进程管理工具supervisor使用指南

Supervisor 是基于 Python 的进程管理工具,只能运行在 Unix-Like 的系统上,也就是无法运行在 Windows 上。Supervisor 官方版目前只能运行在 P...

tensorflow的计算图总结

计算图 在 TensorFlow 中用计算图来表示计算任务。 计算图,是一种有向图,用来定义计算的结构,实际上就是一系列的函数的组合。 用图的方式,用户通过用一些简单的容易理解的数学函...

TensorFlow2.0:张量的合并与分割实例

** 一 tf.concat( ) 函数–合并 ** In [2]: a = tf.ones([4,35,8]) In [3]:...

python遍历文件夹,指定遍历深度与忽略目录的方法

背景 需要在文件夹中搜索某一文件,找到后返回此文件所在目录。用最常规的os.listdir()方式实现了一版,但执行时报错:递归超过最大深度。于是自己添加了点功能,之所有写此函数是为了让...