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

相关文章

opencv python 2D直方图的示例代码

opencv python 2D直方图的示例代码

Histograms - 3 : 2D Histograms 我们已经计算并绘制了一维直方图,因为我们只考虑一个特征,即像素的灰度强度值.但在二维直方图中,需要考虑两个特征,通常,它用...

Python获取当前公网ip并自动断开宽带连接实例代码

今天写了一个获取当前公网ip并且自动断开宽带连接的文件,和大家分享下。 这个文件的具体用途大家懂的,可以尽管拿去用,不过目前只适用于Windows平台,我的Python版本是2.7的,...

将python依赖包打包成window下可执行文件bat方式

1、 打开一个记事本,将需要安装的第三方python依赖包写入文件,比如:需要安装urllib3、flask、bs4三个python库(替换成你想要安装的库,每个库之间用空格隔开),输入...

pandas数据集的端到端处理

1. 数据集基本信息 df = pd.read_csv() df.head():前五行; df.info(): rangeindex:行索引; data columns:列索引...

Python openpyxl读取单元格字体颜色过程解析

问题 我试图打印some_cell.font.color.rgb并得到各种结果。 对于一些人,我得到了我想要的东西(比如“ FF000000”),但对于其他人,它给了我Value mus...